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): 类、结构或接口成员声明中的标记“=”无效
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): 类、结构或接口成员声明中的标记“=”无效
解决方案 »
- visual studio怎么锁定控件(我的是textbox)的位置??使它能够随着窗体的变大而变动。 帮帮我这个菜鸟吧。。。
- 如何调用其他类中的方法
- Mdi窗体问题
- datagridview中单元格的右键菜单事件如何获得用户点击是哪个单元格?
- 看一下这个问题,关于事件的
- 免费 下载使用 学生宿舍管理系统,恭请各位赏脸.
- SQLDMO备份数据库,如果保存路径中有空格会出错,怎么办?
- TreeView问题(WinForm中)
- 如何在一个用户控件上自定义一个事件,然后再该控件窗体上实现该事件,在线等待,谢谢英雄丫
- 如何动态的生成控件数组?
- 谁有Visual C# 2005 Express Edition Beta 2的Activation Key???????
- 瑛勴(CLOUD)来接分。谢谢你的程序。还有100啦。
但还是有问题
把static去掉 加到port上
这个怎么弄? public int static port; 这样是错误的吧
public int GetPort
{
get{return port;}
set{port = value;}
}
public static int GetPort
{
get{ return port; }
set{ port = value; }
}
public static int GetPort
{
get{return port;}
set{port = value;}
}
public static int GetPort
{
get{ return port; }
set{ port = value; }
}
public static int GetPort
{
get{return port;}
set{port = value;}
}
我程序中是这么写的,但是出现了如题所说的错误,是不是我的写法有错误????
private static int port;public static int GetPort
{
get{return port;}
set{port = value;}
}
class2中:
class1.GetPort = 1;//这样写就出错了,提示错误为类、结构或接口成员声明中的标记“=”无效
int i = class1.GetPort;//这样写就正确,能读到class1中的port的值
{
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);
}
}--------------------------------------------
我试过,以上代码编译正确,运行也正确,没问题呀。