动态创建实例的名称 用数组或集合的方法.如: ClassA[] A=new ClassA[8]; A[0] = new ClassA(); A[1] = new ClassA(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我原来提问的帖子http://topic.csdn.net/u/20081202/10/64e172e2-92a3-4a92-a1bb-64d1b4212790.html。你可以参考下 http://topic.csdn.net/t/20050325/08/3879489.html你参考2楼的答案即可! 如果不需要提供设计时的支持,只需要定义一个Collection类 /// <summary> /// ItemCollection /// </summary> public class ItemCollections:System.Collections.CollectionBase { public ItemCollections() { } //定义索引器,这个方法必须有 public MyItem this [int index] { get { return (MyItem)List[index]; } set { List[index] = value; } } public void Add(MyItem item) { this.List.Add(item); } public void Remove(MyItem item) { this.List.Remove(item); } public int IndexOf(MyItem item) { return this.List.IndexOf(item); } protected override void OnInsert(int index, object value) { if(!(value is MyItem)) { throw new ArgumentException("插入Item不正确","未执行"); } } protected override void OnSet(int index, object oldValue, object newValue) { if(!(newValue is MyItem)) { throw new ArgumentException("插入Item不正确","未执行"); } } }class MyItem 相当于你的Class A//使用ItemCollections itemColl = new ItemCollections();MyItem a1 = new ();itemColl.Add(a1);MyItem a2 = new ();itemColl.Add(a2);//MyItem a5 = itemColl[0];// 这里a1和a1指向的是同一个对象要提供设计时支持请参见2楼 可能是我没有把问题说的太明白,我的意思是定义类实例变量(A1,A2,A3,.....)时类实例变量的名字不是写死的,是由其他变量(B和C)组合而成的.运行时根据B和C的值确定类实例变量的名字.如:当B=A,C=1时实例变量为A1 当B=A,C=1时实例变量为A1 当B=A,C=2时实例变量为A2 当B=A,C=3时实例变量为A3 . . . 当B=C,C=E时实例变量为CE 大神,帮我用牛顿法求方程的根!!! 帮忙翻译一个c++的语句到c# 购物车里如何使用2个webconfig进行拦截 每日一学:关于委托 .NET 如何做类似PPS那样的软件 唉~~VS2005问题 自己做了个公司内部浏览器,在弹出窗口时候是搜狗,360,一样!而不是IE那样新开个窗口,请问怎么解决? 32位cpu下编译的dll库能不能在64位下使用! 我已经出离愤怒了,鲁迅先生说过不在沉默中灭亡就在沉默中爆发。。。 用C#如何做输入法呀,给个思路好吗!来者给分。 上传数据时候,只能新加数据,如果是已有的数据就不能更新! 请教捕捉子进程的runtime error
你可以参考下
你参考2楼的答案即可!
/// ItemCollection
/// </summary>
public class ItemCollections:System.Collections.CollectionBase
{
public ItemCollections()
{
} //定义索引器,这个方法必须有
public MyItem this [int index]
{
get
{
return (MyItem)List[index];
}
set
{
List[index] = value;
} }
public void Add(MyItem item)
{
this.List.Add(item);
} public void Remove(MyItem item)
{
this.List.Remove(item);
} public int IndexOf(MyItem item)
{
return this.List.IndexOf(item);
} protected override void OnInsert(int index, object value)
{
if(!(value is MyItem))
{
throw new ArgumentException("插入Item不正确","未执行");
}
} protected override void OnSet(int index, object oldValue, object newValue)
{
if(!(newValue is MyItem))
{
throw new ArgumentException("插入Item不正确","未执行");
}
} }
class MyItem 相当于你的Class A
//使用
ItemCollections itemColl = new ItemCollections();
MyItem a1 = new ();
itemColl.Add(a1);
MyItem a2 = new ();
itemColl.Add(a2);//
MyItem a5 = itemColl[0];
// 这里a1和a1指向的是同一个对象
要提供设计时支持请参见2楼
不是写死的,是由其他变量(B和C)组合而成的.运行时根据B和C的值确定类实例变量的名字.
如:当B=A,C=1时实例变量为A1
当B=A,C=1时实例变量为A1
当B=A,C=2时实例变量为A2
当B=A,C=3时实例变量为A3
.
.
.
当B=C,C=E时实例变量为CE