Click to See Complete Forum and Search --> : Variables between methods.


GunnarJ
November 8th, 2009, 05:24 PM
Hello again. This time I am having problems passing variables between methods.

Example

public static void Method1()
{
int var1 = 1;
}

public static void Method2()
{
int var2 = var1;
}

...

do you see what I am getting at?

trogster
November 8th, 2009, 05:56 PM
I think you should declare the var in the class outside the void.

MNovy
November 9th, 2009, 02:23 AM
use parameters in your methods to pass values


public static void Method1()
{
int var1 = 1;
Method2(var1);
}

public static void Method2(int inputParam)
{
int var2 = inputParam; // var2 is now 1
}

boudino
November 9th, 2009, 03:07 AM
Make it an instance variable

class Foo
{
int var1 = 1;

public static void Method1()
{
var1 = 1;
}

public static void Method2()
{
int var2 = var1;
}
}

TheSamuels
November 9th, 2009, 03:28 AM
I find this just depends how decisive you are, how you change your class to use a global variable.
You might be warned of the value that may have been changed while passing