简体   繁体   中英

Visual C# - Calling function from another *.cs file

I usually been creating small programs, so i never bothered about putting code in few files, but today, i finally decided (as my program has grown) to put some of the functions in other *.cs file, so i would have only button actions handling in form1.cs and i would call functions here, and i would like to have all my functions code in functions.cs for example. That would let me to keep my code little more tidy.

EDIT: I thought thats obvious, that i am asking how to put these functions in other file, and then how to call them, but i see some people don't even try to understand, and just downvote and just trying to be smart. ;) I also thought that "creating small programs" meant, i never had to use any classes, or other stuff like that, as small programs means i am bloody beginner, and i need some guidelines what should i do/READ now, to proceed with my idea.

This is a good idea. You can use the partial class feature of C# to achieve this result.

I know most people dont like explaning to beginners and that is sad. Go here mate it will help you alot as a beginner! It has everything pretty good explained i`m sure it will help you alot.

http://thenewboston.org/list.php?cat=15

Ok, forget about the smart !"#]=)($

what you need to do is create a new .cs file then instantiate a variable and call your functions through this variable:

Create a file myFunctions.cs, and move all your functions there.

then in the other .cs file :

myFunctions var=new myFunctions();

var.funcion1(parameter1);
xx=var.funcion2(parameter1, parameter2);
zz=var.funcion3(parameter1, parameter2, parameter3);

of course is not the idea of classes but it solves your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM