mew903
|
  |
| Joined: 03 Aug 2008 |
| Total Posts: 22071 |
|
|
| 26 Dec 2011 09:38 PM |
As some of you know, today is my first day learning C#
I need to know how to make a void withing a void
Like say, I have a function
private void InitializeComponent() {
}
How would I put another function/void inside of it, like:
private void InitializeComponent() { private void Function2() { }; } |
|
|
| Report Abuse |
|
|
LocalChum
|
  |
| Joined: 04 Mar 2011 |
| Total Posts: 6906 |
|
|
| 26 Dec 2011 09:40 PM |
| You can't. All functions must be within the class declaration. |
|
|
| Report Abuse |
|
|
mew903
|
  |
| Joined: 03 Aug 2008 |
| Total Posts: 22071 |
|
|
| 26 Dec 2011 09:42 PM |
D:
Now my thing is gonna be thousands of lines long :V |
|
|
| Report Abuse |
|
|
1Ra
|
  |
| Joined: 02 May 2010 |
| Total Posts: 2400 |
|
|
| 26 Dec 2011 09:44 PM |
| Now THAT is what she said >:P |
|
|
| Report Abuse |
|
|
mew903
|
  |
| Joined: 03 Aug 2008 |
| Total Posts: 22071 |
|
|
| 26 Dec 2011 09:45 PM |
| 1Ra, go back to farting blood. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 26 Dec 2011 09:46 PM |
| If it needs to be 1000 lines long then you're doing it wrong. Explain what you want to do. |
|
|
| Report Abuse |
|
|
1Ra
|
  |
| Joined: 02 May 2010 |
| Total Posts: 2400 |
|
|
| 26 Dec 2011 09:46 PM |
My history is already written!
i have to go back to the future. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 26 Dec 2011 09:49 PM |
| Can't you use lambdas to achieve something similar? |
|
|
| Report Abuse |
|
|
mew903
|
  |
| Joined: 03 Aug 2008 |
| Total Posts: 22071 |
|
|
| 26 Dec 2011 09:49 PM |
@xSTRAVANTx
This is what I want .... (ignore variables)
using System; using System.Drawing; using System.Windows.Forms;
public class frmMain : Form { #region Windows code private void InitializeComponent() { private void Function2() { // Errors :'( } } #endregion
public frmMain() { InitializeComponent(); }
public static void Main() { frmMain main = new frmMain(); Application.Run(main); } } |
|
|
| Report Abuse |
|
|
mew903
|
  |
| Joined: 03 Aug 2008 |
| Total Posts: 22071 |
|
| |
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 26 Dec 2011 09:56 PM |
| Why do you need it to be that way though. You don't do things that way in static languages like C#. |
|
|
| Report Abuse |
|
|
mew903
|
  |
| Joined: 03 Aug 2008 |
| Total Posts: 22071 |
|
|
| 26 Dec 2011 09:57 PM |
| Well, can I access variables made in one private void in another? |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 26 Dec 2011 10:01 PM |
| You pass it as an argument to the function, yes. |
|
|
| Report Abuse |
|
|
mew903
|
  |
| Joined: 03 Aug 2008 |
| Total Posts: 22071 |
|
|
| 26 Dec 2011 10:03 PM |
| Help? (might be latetoast) |
|
|
| Report Abuse |
|
|
|
| 26 Dec 2011 10:05 PM |
"Well, can I access variables made in one private void in another?"
Pass by reference if you want to modify the values inside the other function without having to return anything. Example:
private void SomeFunc(ref int firstVar, ref int secondVar, ref int thirdVar) { firstVar = secondVar + thirdVar; secondVar = firstVar - thidVar; thirdVar += firstVar * secondVar; }
This modifies each variable, and when you return to your main function the variables will remain modified. However, this way seems less neat if you're passing multiple values but only need to return one value, in which case you're better off doing something like:
int SomeVar = 5; SomeVar += SomeOtherFunction(3, 6);
...
private int SomeOtherFunction(int FirstVar, int SecondVar) { return FirstVar + SecondVar; } |
|
|
| Report Abuse |
|
|
|
| 26 Dec 2011 10:06 PM |
| And I forgot to say you can use that to build off of if you need to return nothing at all, and you just need the values of the variables. |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 27 Dec 2011 12:29 AM |
"Well, can I access variables made in one private void in another?"
Make them member variables of the class. Welcome to OOP. |
|
|
| Report Abuse |
|
|
|
| 28 Mar 2013 02:09 PM |
I don't know if you resolved this but here.
public static int a = 1; //THIS IS A GLOBAL VARIABLE SO ALL FUNCTIONS CAN USE IT public static void main(String[] args) { }
public static void second() { a = 2;//CALLS THE GLOBAL VARIABLE AND CHANGES IT } |
|
|
| Report Abuse |
|
|
| |
|
BAUER102
|
  |
| Joined: 03 Apr 2010 |
| Total Posts: 5936 |
|
| |
|
|
| 28 Mar 2013 02:19 PM |
| Lol that was a large bump... |
|
|
| Report Abuse |
|
|
|
| 28 Mar 2013 02:21 PM |
The people you're helping are essentially an internet form of dead.
Dead people are: -Not likely to move -Not likely to speak -Not likely to ever be alive again
The dead are past help. |
|
|
| Report Abuse |
|
|