C# 如何从类返回包含的对象集合
我有一个类变量和另一个类变量。 Variables类用于维护和生成类型变量的对象列表 一切正常,我很高兴。但是有一件事我不知道怎么做 我想像这样使用foreach循环中的变量C# 如何从类返回包含的对象集合,c#,.net,vb.net,C#,.net,Vb.net,我有一个类变量和另一个类变量。 Variables类用于维护和生成类型变量的对象列表 一切正常,我很高兴。但是有一件事我不知道怎么做 我想像这样使用foreach循环中的变量 Variables oVariables; foreach(Variable element in oVariables) 但是我不能仅仅通过使用这个例子中的类变量的对象来实现这一点,这个类变量是可修改的。我必须编写一些函数来返回集合。 那么有没有什么方法可以避免编写额外的函数并从中获取集合呢 感谢您的帮助。谢谢 下面是
Variables oVariables;
foreach(Variable element in oVariables)
但是我不能仅仅通过使用这个例子中的类变量的对象来实现这一点,这个类变量是可修改的。我必须编写一些函数来返回集合。
那么有没有什么方法可以避免编写额外的函数并从中获取集合呢
感谢您的帮助。谢谢
下面是变量类的代码
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualBasic;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace APVariable
{
public class Variables
{
private Collection mCol;
public Variable Add(string Name, object Value, string skey = "")
{
Variable objNewMember = new Variable();
objNewMember.Name = Name;
objNewMember.Value = Value;
if (skey.Length == 0)
{
mCol.Add(objNewMember);
}
else
{
try
{
Information.Err().Clear();
mCol.Add(objNewMember, skey);
if (Information.Err().Number != 0)
{
Information.Err().Clear();
mCol.Add(objNewMember);
}
}
catch { Information.Err(); }
{
}
}
return objNewMember;
objNewMember = null;
}
public int count
{
get
{
return mCol.Count;
}
}
//public void Remove(int vntIndexKey)
//{
// //this can be the int or the string.
// //passes the index or the key of the collection to be removed.
// mCol.Remove(vntIndexKey);
//}
public void Remove(dynamic vntIndexKey)
{
//this can be the int or the string.
//passes the index or the key of the collection to be removed.
mCol.Remove(vntIndexKey);
}
public dynamic newEnum
{
get
{
return mCol.GetEnumerator();
}
}
public Variable this[object vIndex]
{
get
{
Variable result = null;
try
{
result = (Variable)mCol[vIndex];
}
catch
{
}
return result;
}
}
//public IEnumerator GetEnumerator()
//{
// //this property allows you to enumerate
// //this collection with the For...Each syntax
// return mCol.GetEnumerator();
//}
public Variables()
{
mCol = new Collection();
}
~Variables()
{
mCol = null;
}
}
}
您需要实现该接口。这将允许您在类上使用foreach。你也应该使用 看起来你已经开始了,你就快到了
namespace APVariable
{
public class Variables : IEnumerable, IEnumerator
{
private Collection<Variable> mCol;
public Variable Add(string Name, object Value, string skey = "")
{
Variable objNewMember = new Variable();
objNewMember.Name = Name;
objNewMember.Value = Value;
if (skey.Length == 0)
{
mCol.Add(objNewMember);
}
else
{
try
{
Information.Err().Clear();
mCol.Add(objNewMember, skey);
if (Information.Err().Number != 0)
{
Information.Err().Clear();
mCol.Add(objNewMember);
}
}
catch { Information.Err(); }
{
}
}
return objNewMember;
objNewMember = null;
}
public int count
{
get
{
return mCol.Count;
}
}
//public void Remove(int vntIndexKey)
//{
// //this can be the int or the string.
// //passes the index or the key of the collection to be removed.
// mCol.Remove(vntIndexKey);
//}
public void Remove(dynamic vntIndexKey)
{
//this can be the int or the string.
//passes the index or the key of the collection to be removed.
mCol.Remove(vntIndexKey);
}
public dynamic newEnum
{
get
{
return mCol.GetEnumerator();
}
}
public Variable this[object vIndex]
{
get
{
Variable result = null;
try
{
result = (Variable)mCol[vIndex];
}
catch
{
}
return result;
}
}
public IEnumerator GetEnumerator()
{
//this property allows you to enumerate
//this collection with the For...Each syntax
return mCol.GetEnumerator();
}
public Variables()
{
mCol = new Collection<Variable>();
}
~Variables()
{
mCol = null;
}
}
}
</code>
</pre>
名称空间变量
{
公共类变量:IEnumerable、IEnumerator
{
私人收藏mCol;
公共变量Add(字符串名称、对象值、字符串skey=”“)
{
变量objNewMember=新变量();
objNewMember.Name=名称;
objNewMember.Value=值;
如果(skey.Length==0)
{
mCol.Add(对象成员);
}
其他的
{
尝试
{
Information.Err().Clear();
mCol.Add(对象成员,skey);
if(Information.Err().Number!=0)
{
Information.Err().Clear();
mCol.Add(对象成员);
}
}
catch{Information.Err();}
{
}
}
返回对象成员;
objNewMember=null;
}
公共整数计数
{
收到
{
返回mCol.Count;
}
}
//公共无效删除(int-vntIndexKey)
//{
////这可以是int或字符串。
////传递要删除的集合的索引或键。
//mCol.Remove(vntIndexKey);
//}
公共无效删除(动态vntIndexKey)
{
//这可以是int或字符串。
//传递要删除的集合的索引或键。
mCol.Remove(vntIndexKey);
}
公共动态新枚举
{
收到
{
返回mCol.GetEnumerator();
}
}
公共变量this[object vIndex]
{
收到
{
变量结果=null;
尝试
{
结果=(变量)mCol[vIndex];
}
接住
{
}
返回结果;
}
}
公共IEnumerator GetEnumerator()
{
//此属性允许您枚举
//此集合包含For…每个语法
返回mCol.GetEnumerator();
}
公共变量()
{
mCol=新集合();
}
~Variables()
{
mCol=null;
}
}
}
我建议对集合或列表进行子类化,或者实现IList或ICollection谢谢Dan。成功了。我实现了IEnumerable和GetEnumerator,现在一切正常。你能告诉我实现ICollection的原因吗。。。我不明白?抱歉,我的意思是
集合
这样,您的备份存储是强类型的,在迭代时,您不需要将type对象
强制转换为type变量
@拉希拉赫玛德