'IEnumerable does not allow access to sub items
To make a long story short. I have the following code:
class MyList : IEnumerable
{
private List<string> T1 = new List<string>();
private List<string> T2 = new List<string>();
private List<string> T3 = new List<string>();
public List<string> Name { set { T1 = value; } get { return T1; } }
public List<string> DataType { set { T2 = value; } get { return T2; } }
public List<string> Nullable { set { T3 = value; } get { return T3; } }
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public MyList<List<string>, List<string>, List<string>> GetEnumerator()
{
return new MyList<List<string>, List<string>, List<string>>(T1, T2, T2);
}
}
What I want is to access it like this:
MyList ml = new MyList();
foreach (var item in ml)
{
str = item.Name;
}
It does not let me access the subitem, like item.Name, or item.DataType.
Solution 1:[1]
It seems you are trying to make a list of things, each of which has 3 properties: Name, DataType, Nullable. Try this:
class MyItem
{
public string Name { get; set; }
public string DataType { get; set; }
public string Nullable { get; set; } // also, consider making this bool
}
Then make a list of those.
Solution 2:[2]
Based on the answer from @CSJ, I would suggest:
public class MyItem
{
public string Name { get; set; }
public string DataType { get; set; }
public string Nullable { get; set; } // also, consider making this bool
}
public class MyItemList : IEnumerable<MyItem>
{
private List<MyItem> MyItems = new List<MyItem>();
public IEnumerator<MyItem> GetEnumerator()
{
return this.MyItems.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CSJ |
| Solution 2 | Bender the Greatest |
