class Example
{
int sumAll=0;
void firstMethod()
{
sumAll=function();
...
}
void anotherMethod()
{
int x = sumAll (此sumAll的值为void firstMethod()函数得到的值)
}
} 其中void firstMethod()和void anotherMethod()都不能用形参,情况如何定义sumAll变量,如何传递,非常感谢
{
int sumAll=0;
void firstMethod()
{
sumAll=function();
...
}
void anotherMethod()
{
int x = sumAll (此sumAll的值为void firstMethod()函数得到的值)
}
} 其中void firstMethod()和void anotherMethod()都不能用形参,情况如何定义sumAll变量,如何传递,非常感谢
public int sumAll=0;
其他的不变,就可以了。
namespace ConsoleApplication4
{
class Program
{
public int Tmp = 0;
private int Fuc1()
{
Tmp = 5;
return Tmp;
}
private int Fuc2()
{
int Tmpp = Tmp;
return Tmpp;
}
static void Main(string[] args)
{
Program pr = new Program();
Console.WriteLine(pr.Fuc1());
Console.WriteLine(pr.Fuc2());
Console.ReadKey();
}
}
}
lz代码问题在于anotherMethod没有调用firstMethod去改变sumAll这个变量。
那两个方法声明称public
public void firstMethod()
{}public void secondMethod()
{}Example ex=new Example();
ex.firstMethod();
ex.secondMethod();//这样就可以了
声明了方法,如果不调用,全局变量的值是肯定不会变的。
public class Program
{
private int sum = 0; static void Main()
{
Program prog = new Program();
prog.anotherM();
} void firstM()
{
sum = sum + 5;
} void anotherM()
{
firstM();
Console.WriteLine(sum);
}
}