|
-
November 8th, 2009, 06:24 PM
#1
Variables between methods.
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?
-
November 8th, 2009, 06:56 PM
#2
Re: Variables between methods.
I think you should declare the var in the class outside the void.
-
November 9th, 2009, 03:23 AM
#3
Re: Variables between methods.
use parameters in your methods to pass values
Code:
public static void Method1()
{
int var1 = 1;
Method2(var1);
}
public static void Method2(int inputParam)
{
int var2 = inputParam; // var2 is now 1
}
Last edited by MNovy; November 9th, 2009 at 03:30 AM.
-
November 9th, 2009, 04:07 AM
#4
Re: Variables between methods.
Make it an instance variable
Code:
class Foo
{
int var1 = 1;
public static void Method1()
{
var1 = 1;
}
public static void Method2()
{
int var2 = var1;
}
}
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
November 9th, 2009, 04:28 AM
#5
Re: Variables between methods.
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|