AceInfinity
Emeritus, Contributor
I've constructed two classes for this little example. What we're doing here has to deal with the concept of interfaces in C#. If you look at the code below you'll see that for the class ForumCommunity, I've implemented the IEnumerable interface, and we have another class which is utilized by ForumCommunity, which doesn't implement anything, except System.Object implicitly because of the hierarchal paradigm of the .NET Framework library.
Most of this demonstration to be able to understand what's going on here, is in the comments I've put into my code. CommunityList isn't really useful for this example, I could have left it out, I just kept it there as good cause for a little bit of the OOC concept with encapsulation mainly.
In the Member class, that was a bit of fooling around as well, the only real place where a Member is assignable is through the constructor. Ignore all that, that's just a bit of fooling around on my end, but for the main example here what I'm trying to show you guys is the usefulness of interfaces.
Of course here, this isn't even scratching the surface, as we're not creating our own interface, and the IEnumerable interface is one of the most easily understood interfaces in my opinion in the BCL. You've got GetEnumerator() (which is what we're making use of in the code below).
I'll first demonstrate how to iterate through each Member in the List<Member> for the ForumCommunity class defined by the private Community object.
Since this class extends the GetEnumerator method returning an IEnumerator interface, note the beauty now, in that we can also use the foreach construct in order to loop through each Member object in our List<Member> variable; Community.
:beerchug2:
~Ace
Most of this demonstration to be able to understand what's going on here, is in the comments I've put into my code. CommunityList isn't really useful for this example, I could have left it out, I just kept it there as good cause for a little bit of the OOC concept with encapsulation mainly.
In the Member class, that was a bit of fooling around as well, the only real place where a Member is assignable is through the constructor. Ignore all that, that's just a bit of fooling around on my end, but for the main example here what I'm trying to show you guys is the usefulness of interfaces.
Of course here, this isn't even scratching the surface, as we're not creating our own interface, and the IEnumerable interface is one of the most easily understood interfaces in my opinion in the BCL. You've got GetEnumerator() (which is what we're making use of in the code below).
C#:
public class ForumCommunity : IEnumerable
{
//System.Collections.Generic.List already implements IEnumerator
//Only need to implement IEnumerable interface and we'll use the GetEnumerator method
private List<Member> Community = new List<Member>();
public List<Member> CommunityList { get { return Community; } }
//ForumCommunity Constructor
public ForumCommunity()
{
Community.Add(new Member("AceInfinity", 1));
Community.Add(new Member("KoBE", 2));
Community.Add(new Member("Unknown1", 5038222));
Community.Add(new Member("Bob", 0));
Community.Add(new Member("John", 10000));
}
public IEnumerator GetEnumerator()
{
// Return the generic list's IEnumerator.
return Community.GetEnumerator();
}
}
public class Member
{
//Member Constructor
public Member(string Name, int UID)
{
_Name = Name;
_UID = UID;
}
//Members of the class
public string _Name { get; private set; }
public int _UID { get; private set; }
}
I'll first demonstrate how to iterate through each Member in the List<Member> for the ForumCommunity class defined by the private Community object.
C#:
private void MainMethod()
{
ForumCommunity FC = new ForumCommunity();
IEnumerator Iterator = FC.GetEnumerator();
while (Iterator.MoveNext())
{
Member curMember = ((Member)Iterator.Current);
Console.WriteLine(string.Format("The member {0} has a UID of {1}.", curMember._Name, curMember._UID));
}
}
Since this class extends the GetEnumerator method returning an IEnumerator interface, note the beauty now, in that we can also use the foreach construct in order to loop through each Member object in our List<Member> variable; Community.
C#:
private void MainMethod()
{
ForumCommunity FC = new ForumCommunity();
foreach (Member curMember in FC)
Console.WriteLine(string.Format("The member {0} has a UID of {1}.", curMember._Name, curMember._UID));
}
}
:beerchug2:
~Ace