0
클래스 변수 및 다른 클래스 변수가 있습니다. Variables 클래스는 Variable 형식의 개체 목록을 유지 관리하고 생성하는 데 사용됩니다.클래스에서 포함 된 객체의 컬렉션을 반환하는 방법
모든 것이 정상적으로 작동하며 만족 스럽습니다. 그러나 그것을 끝내는 방법을 모르는 한 가지가 있습니다.
은 내가
Variables oVariables;
foreach(Variable element in oVariables)
.. 같이 foreach 루프에서 변수를 사용하려면하지만 난 그냥 oVariable이다이 예에서 클래스 변수의 개체를 사용하여이 작업을 수행 할 수 없습니다입니다. 컬렉션을 반환하는 몇 가지 함수를 작성해야합니다. 그래서 나는 여분의 함수를 쓰지 않고 Collection을 얻을 수있는 방법이있다.
도움이 되셨습니까? 감사합니다 여기에 변수 클래스의 코드가 있습니다.
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;
}
}
}
댄 감사 하위 클래스 컬렉션 또는 목록 또는 구현은 IList 또는 ICollection은 권하고 싶습니다. 그게 효과가 있었어. 나는 IEnumerable과 GetEnumerator를 구현했으며 모든 것이 이제는 잘 작동한다. –
ICollection 구현의 이유를 말해 줄 수 있습니까? –
죄송합니다. 'Collection'을 의미합니다.이 방법을 사용하면 백킹 스토어에 강력한 형식이 지정되며 반복 할 때'variable'을 입력하기 위해'object' 유형을 형 변환 할 필요가 없습니다. @ 라 헤르 마흐 –