| | |
procedures and functions
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2007
Posts: 266
Reputation:
Solved Threads: 3
is there any way that one can place code in C# in a procedure or a function? I have code logic which is going to be executed many times over and instead of recoding it twenty times over I want to put them in functions and procedures so that I can just invoke them all the time. The help file only refers to procedures and functions in server 200 or later. is there not someting in c# itself?
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
90% of what you do in C# (or any OO language for that matter) is in functions.
I am not sure exactly what you are asking though - I know you are new to C#. You ask if you can put some repetative code in a function and call it over and over instead of coding it everytime. But coding it every time is writing a function every time and calling that code is calling a function - so it is the same principal in c#.
If you could be a little bit clearer or give some small code of what you are trying to do I will give you a better answer to get you on your way.
I am not sure exactly what you are asking though - I know you are new to C#. You ask if you can put some repetative code in a function and call it over and over instead of coding it everytime. But coding it every time is writing a function every time and calling that code is calling a function - so it is the same principal in c#.
If you could be a little bit clearer or give some small code of what you are trying to do I will give you a better answer to get you on your way.
•
•
Join Date: May 2007
Posts: 266
Reputation:
Solved Threads: 3
consider the following c++ code. It contains a function called square wich can be called hundreds of times without the programmer having to code it one hundred times. main calls it only once but main could have called it as often as it wished.
int square( int )
int main()
{
int number;
cin >> number;
cout << square( number ) << endl;
return 0;
}
int square ( int n )
{
return n * n;
}
i would like to know the C# counterpart of that. But i would be calling the square function from a command button on a form. In Visual Basic you can add a new module which can be either a function( i.e. return values ) or a procedure ( not return values ). Then when you click a button in VB you say ...Call procedure... in the event procedure of the button or if you want the output from a function you just assign the function to variable also somewhere in code. How do I do the equivalent in C#?
Thank you very much for your help.
int square( int )
int main()
{
int number;
cin >> number;
cout << square( number ) << endl;
return 0;
}
int square ( int n )
{
return n * n;
}
i would like to know the C# counterpart of that. But i would be calling the square function from a command button on a form. In Visual Basic you can add a new module which can be either a function( i.e. return values ) or a procedure ( not return values ). Then when you click a button in VB you say ...Call procedure... in the event procedure of the button or if you want the output from a function you just assign the function to variable also somewhere in code. How do I do the equivalent in C#?
Thank you very much for your help.
You code it exactly like you have written it 
If you need to call it from a button, then in your button click handler you merely call the function and update the value wherever you need to (i.e. a textfield or whatever).
C# doesn't have "procedures" - only functions. Functions that do not return a value are declared as returning "void", like so

If you need to call it from a button, then in your button click handler you merely call the function and update the value wherever you need to (i.e. a textfield or whatever).
C# doesn't have "procedures" - only functions. Functions that do not return a value are declared as returning "void", like so
C# Syntax (Toggle Plain Text)
public void someFunction(){ }
![]() |
Similar Threads
- Sorting in date order (Visual Basic 4 / 5 / 6)
- Declaring functions (PHP)
- PHP, ASP, ColdFusion, what's your fav? (IT Professionals' Lounge)
- Unable to insert data into SQL Database (ASP)
- VB's Left, Right, Mid Functions in C++? (C++)
- SQL Server vs MYSQL vs MSQL (i'm stopping now) (MS SQL)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
Other Threads in the C# Forum
- Previous Thread: argument list for system.diagnostics.process
- Next Thread: Creating an ACPI script for my laptop
| Thread Tools | Search this Thread |
.net access algorithm array asp.net barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net httpwebrequest image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql mysql.data.client operator path photoshop php picturebox pixelinversion post programming radians regex remoting resourcefile richtextbox server sleep socket sql statistics stream string study system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml






