class1中我写了个属性
private int port;
public static int GetPort
{
get{return port;}
set{port = value;}
}
class2中,我想给这个属性GetPort赋个值,但由于属性是个静态的,所以没有创建类的实例,直接用class1.GetPort来访问,但是,这样就不能给属性赋值,应该怎样才能赋上值呢? 比如说我在class2中引用class1中的这个属性
int i = class1.GetPort;//这个是正确的
但是 class1.GetPort = 1;//这个是错的 
错误提示:
E:\TCP\UDP\UDP\UDPEngine.cs(13): 类、结构或接口成员声明中的标记“=”无效

解决方案 »

  1.   

    public static int port
      

  2.   

    我程序中写的是 public static int port ;
    但还是有问题
      

  3.   

    把static去掉  加到port上
      

  4.   

    回复人: mulangren(牧狼人) ( ) 信誉:100  2005-04-25 16:09:00  得分: 0  
     
     
       把static去掉  加到port上
      
     
    这个怎么弄?  public int static port; 这样是错误的吧
      

  5.   

    private static int port;
    public int GetPort
    {
    get{return port;}
    set{port = value;}
    }
      

  6.   

    为什么要给static 变量在其他类中赋值,不理解!!!!
      

  7.   

    private static int port;
    public  static int GetPort
    {
      get{ return port;  }
      set{ port = value; }
    }
      

  8.   

    如果改成public int GetPort{...}的话,我引用的时候还得实例化这个类,我现在想的就是用表态的属性,但不知道怎么弄,这样写就出错.我要保留public static int GetPort{.....},应该怎么办呢?
      

  9.   

    赋值的原因是,在别的类中还需要用到port的值,所以,要通过这个类通过属性改变port值,其它的类再通过访问该属性,能得到最新的值
      

  10.   

    private static int port;
    public static int GetPort
    {
    get{return port;}
    set{port = value;}
    }
      

  11.   

    private static int port;
    public  static int GetPort
    {
      get{ return port;  }
      set{ port = value; }
    }
      

  12.   

    private static int port;
    public static int GetPort
    {
    get{return port;}
    set{port = value;}
    }
     我程序中是这么写的,但是出现了如题所说的错误,是不是我的写法有错误????
      

  13.   

    比如说吧,静态方法,可以不实例化类,而直接调用(比如Console.Write),所以,我想不通过实例化,用静态属性改变他的值,但发现出错了,解决不了.
      

  14.   

    class1中:
    private static int port;public static int GetPort
    {
    get{return port;}
    set{port = value;}
    }
    class2中:
    class1.GetPort = 1;//这样写就出错了,提示错误为类、结构或接口成员声明中的标记“=”无效 
    int i = class1.GetPort;//这样写就正确,能读到class1中的port的值
      

  15.   

    class class1
    {
      private static int port;
      public  static int GetPort
      {
        get{ return port;  }
        set{ port = value; }
      }
    }class class2
    {
      static void Main()
      {
        int i = class1.GetPort;
        class1.GetPort = 2;
        System.Console.WriteLine(class1.GetPort);
      }
    }--------------------------------------------
    我试过,以上代码编译正确,运行也正确,没问题呀。