| | |
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 ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mailmerge mandelbrot math mouseclick mysql networking operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox robot save saving serialization server sleep socket sql statistics stream string stringformatting table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






