I have a property in VB6 that I am trying to convert to C#. It is as follows:
Public Property Get NewEnum() As IUnknown
'this property allows you to enumerate
'this collection with the For...Each syntax
Set NewEnum = m_coll.[_NewEnum]
End Property
m_coll
is private variable that is now an ArrayList
instead of the former Collection
.
m_coll
is being populated with one of my own class objects. This property is of the type IUnknown as you can see.
I may just not be thinking properly at this point, but is there an equivalent to this sort of property in C#?
If you want to be able to do a foreach over a class (like you could by exposing NewEnum() as IUnknown in vb6) you can have your class implement IEnumerable - e.g.:
public class MyClass : IEnumerable
{
private List<string> items = new List<string>();
public MyClass()
{
items.Add("first");
items.Add("second");
}
public IEnumerator GetEnumerator()
{
return items.GetEnumerator();
}
}
which would allow you to use it like this:
MyClass myClass =new MyClass();
foreach (var itm in myClass)
{
Console.WriteLine(itm);
}
I've used List<string>
for simplicity, but you can use List<yourCustomClass>