static void Main(string[] args)
        {
            point p1=new point (2,3);
            point.twice(out p, p1);
            Console.WriteLine(p.x);
            Console.WriteLine(p.y);
            Console.ReadLine();
        }
class point
    {
        public double x;
        public double y;
        public point(double a, double b)
        {
            x = a;
            y = b;
        }        public static void twice(out point p,point pp)
        {
            p.x = pp.x*2;
            p.y = pp.y*2;        }
    }总是报错“使用了未赋值的变量‘p’”
请问如何修改,将输入点的X,y扩大两倍,通过OUT方式传出

解决方案 »

  1.   

     point.twice(out p, p1); 这里的out p你肯定没有赋值呀,你应该这样声明:point p=new point(0,0);
      

  2.   

    p是什么东西?
    加上Point p=new Point(0,0);试试
      

  3.   

    out 在函数结束时必须重新赋值
      

  4.   

    按照你的需求, 貌似应该这样
      public static void twice(ref point p) 
      {
        if (p == null)
          return;
        p.x *= 2.0; 
        p.y *= 2.0; 
      }
      

  5.   

    楼主请注意你的编码规范
    class Demo
    {
        static void Main(string[] args)
        {
            Point p1 = new Point(2, 3);
            Point p;
            Point.Twice(out p, p1);
            Console.WriteLine(p.x);
            Console.WriteLine(p.y);
            Console.ReadLine();
        }
    }class Point
    {
        public double x;
        public double y;
        public Point(double a, double b)
        {
            x = a;
            y = b;
        }    public static void Twice(out Point p, Point pp)
        {
            p = new Point(pp.x * 2, pp.y * 2);
        }
    }
      

  6.   

    out是不用初始化的
    这是ref和out的区别
      

  7.   

    it is easier to read using return value:        public static Point DoubleUp(Point p) 
            { 
                return new Point(p.x * 2, p.y * 2);
            } 
      

  8.   

    out函数结束的时候必须赋值using system;
    class testapp
    {
     static void outtest(out int x, out int y)
     {//离开这个函数前,必须对x和y赋值,否则会报错。 
      //y = x; 
      //上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行 
      x = 1;
      y = 2;
     }
     static void reftest(ref int x, ref int y)
     { 
      x = 1;
      y = x;
     }
     public static void Main()
     {
      //out test
      int a,b;
      //out使用前,变量可以不赋值
      outtest(out a, out b);
      console.writeline("a={0};b={1}",a,b);
      int c=11,d=22;
      outtest(out c, out d);
      Console.WriteLine("c={0};d={1}",c,d);  //ref test
      int m,n;
      //reftest(ref m, ref n); 
      //上面这行会出错,ref使用前,变量必须赋值  int o=11,p=22;
      reftest(ref o, ref p);
      Console.WriteLine("o={0};p={1}",o,p);
     }