class Example
{
    int sumAll=0;
    void firstMethod()
    {
        sumAll=function();
        ...
    }
    void anotherMethod()
    {
        int x = sumAll  (此sumAll的值为void firstMethod()函数得到的值)
    }
} 其中void firstMethod()和void anotherMethod()都不能用形参,情况如何定义sumAll变量,如何传递,非常感谢

解决方案 »

  1.   

    把sumAll声明为全局变量就可以了
    public int sumAll=0; 
    其他的不变,就可以了。
     
      

  2.   

    我写的一个简单的示例,你看一下,结果是5,5
    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();
            }
                }
    }
      

  3.   

    在类的内部不用声明为public吧。
    lz代码问题在于anotherMethod没有调用firstMethod去改变sumAll这个变量。
      

  4.   

    只在内部调用的话,不用Public也是可以的.
    那两个方法声明称public
    public void firstMethod()
    {}public void secondMethod()
    {}Example ex=new Example();
    ex.firstMethod();
    ex.secondMethod();//这样就可以了
      

  5.   

    类的内部使用,方法也不需要声明public的,lz问题在于红色部分代码。
    声明了方法,如果不调用,全局变量的值是肯定不会变的。
    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);
            }
            
        }