急!!!马上结贴!

marty2001 2004-12-09 10:51:54
结构成员名可以定义成变量吗?如可以请问怎么实现。 例如:
public struct Region1
{
public static int[] ID;
public static string[] Item;
public static bool[] Default;
public static int Count;
}
public struct Region2
{
public static int[] ID;
public static string[] Item;
public static bool[] Default;
public static int Count;
}
public struct Region3
{
public static int[] ID;
public static string[] Item;
public static bool[] Default;
public static int Count;
}

调用以上结构成员:
Region1.Count = 1;
Region2.Count = 1;
Region3.Count = 1;

如何将上面3句重复的话写成一个带参数的方法调用,简化多余的代码。
...全文
205 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
marty2001 2004-12-20
  • 打赏
  • 举报
回复
up
ffb 2004-12-09
  • 打赏
  • 举报
回复
三个结构体不是完全一样吗?用一个不就行了
youhj 2004-12-09
  • 打赏
  • 举报
回复
同意 luaiping(风起云涌) 的写法。
amendajing 2004-12-09
  • 打赏
  • 举报
回复
mark
marty2001 2004-12-09
  • 打赏
  • 举报
回复
编译出错
public struct kk
{
public static int[] ID;
public static string[] Item;
public static bool[] Default;
public static int Count;

public kk(){}
}

public static kk k1 = new kk();

“public kk(){}”结构不能包含显式的无参数构造函数
lr2651 2004-12-09
  • 打赏
  • 举报
回复
和声明类及类的实例一样的,C++及C#里类的灵感就是来字C的结构的。
marty2001 2004-12-09
  • 打赏
  • 举报
回复
我现在这样做,但是在别的窗体调用的时候不知道怎么才能实现:k1.Count,好像在K1后按“.”没东西可选
public struct kk
{
public static int[] ID;
public static string[] Item;
public static bool[] Default;
public static int Count;

public kk(){}
}

public static kk k1 = new kk();
marvelstack 2004-12-09
  • 打赏
  • 举报
回复
使用结构数组
Region [] regions = new Region[3];
regions[0] = regions1;
regions[1] = regions2;
regions[2] = regions3;
luaiping 2004-12-09
  • 打赏
  • 举报
回复
可以啊,你可以把他定义成公用的呀
marty2001 2004-12-09
  • 打赏
  • 举报
回复
我要定义的是公用变量,要在任何一个窗体都能调用到它的值
Region region1 = new Region()
Region region2 = new Region()
Region region3 = new Region()
可以吗?
linlinunix 2004-12-09
  • 打赏
  • 举报
回复
luaiping(风起云涌)
的写法没有问题可以实现
luaiping 2004-12-09
  • 打赏
  • 举报
回复
那按我那样定义就可以啊
marty2001 2004-12-09
  • 打赏
  • 举报
回复
不是那个意思,我是想像以下的结果一样:
private void aa(Region x,int i)
{
x.Count = i;
}

aa(Region1,5);
aa(Region2,8);
aa(Region3,10);
fanruinet 2004-12-09
  • 打赏
  • 举报
回复
写出这样的方法可不能“简化多余的代码”啊,一个方法比3次赋值麻烦多了
linlinunix 2004-12-09
  • 打赏
  • 举报
回复
public class region
{
public Region
{
public int[] ID;
public string[] Item;
public bool[] Default;
public int Count;
}
}
public class App
{
private System.ComponentModel.Container myContainer;
public app()
{
myContainer = new System.ComponentModel.Container();

}


public void InstanceNewRegion()
{
region temp = new region();
temp.Count = 1;
myContainer.Add(temp);
}
}
fengyv 2004-12-09
  • 打赏
  • 举报
回复
up
luaiping 2004-12-09
  • 打赏
  • 举报
回复
Region region1 = new Region()
Region region2 = new Region()
Region region3 = new Region()
marty2001 2004-12-09
  • 打赏
  • 举报
回复
参数就是Region1、Region2、Region3等任意一个
luaiping 2004-12-09
  • 打赏
  • 举报
回复
public struct Region
{

public int[] ID;
public string[] Item;
public bool[] Default;
public int Count;

public Region()
{}
}

象类一样实例化使用
linlinunix 2004-12-09
  • 打赏
  • 举报
回复
你要实现什么功能
单独做一个类不就可以了
然后任意实例化多个用容器装起来任意调用
加载更多回复(11)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧