?
Solved

Make a Struct behave like a collection

Posted on 2005-04-20
13
Medium Priority
?
156 Views
Last Modified: 2010-04-16
Hello

I have a simple struct and i want to add to it the collection's functionality, so i can say then

foreach (int component in myStruct)
{
  Console.Write(component);
}

I know i have to implement 2 interfaces for this, but i dont know how. Can you show me please?

struct myStruct
{
  int a,b,c;

  public myStruct(int a, int b, int c)
 {
   this.a = a;
   this.b = b;
   this.c = c;
  }
}
0
Comment
Question by:Kokas79
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 11

Expert Comment

by:Agarici
ID: 13823047
you should get a type object from myStruct
the type class has methods for enumerating fields, functions etc.

hth,
A.
0
 
LVL 8

Assisted Solution

by:rajaloysious
rajaloysious earned 200 total points
ID: 13823069
see if this is helpful..
cheers
            public class StructCollection : System.Collections.CollectionBase
            {
                  // Restricts to myStruct types, items that can be added to the collection
                  public void Add(myStruct )
                  {
                        List.Add(myStruct );
                  }
                  public myStruct Item(int Index)
                  {
                        // The appropriate item is retrieved from the List object and
                        // explicitly cast to the myStruct type, then returned to the
                        // caller.
                        return (myStruct) List[Index];
                  }
                  public void Remove(int index)
                  {
                        // Check to see if there is a widget at the supplied index.
                        if  (index > Count - 1 || index < 0)
                              // If no CColumnInfo exists, do nothing
                        {
                              //Do nothing
                        }
                        else
                        {
                              List.RemoveAt(index);
                        }
                  }
0
 

Author Comment

by:Kokas79
ID: 13823114
Agarici: sorry but i dont understand what u saying

rajaloysious: thanks for the solution, but not what i'm looking for at the moment

what i want to do is:

public struct myStruct : IEnumerable
{
  ....override all members of that interface here
}

but i'm not a capable programmer yet to do this!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:rajaloysious
ID: 13823115
Oops, i misunderstood the Q...
0
 

Author Comment

by:Kokas79
ID: 13823118
don't worry...i will keep your answer as an alternative solution :)

thanks
0
 
LVL 8

Accepted Solution

by:
Razzie_ earned 800 total points
ID: 13823428
The easiest to do this might be to give your struct a private ArrayList to store items so you would get something like:

struct MyStruct : IEnumerable
{
   private ArrayList arrayList = new ArrayList();

   public void Add(Object o)
   {
      this.arrayList.Add(o);
   }

   public IEnumerator GetEnumerator()
   {
      return this.arrayList.GetEnumerator();
   }
}


With this code you add items to your struct using myStruct.Add() AND use your struct in a foreach() statement.
However, this way your struct isn't really a collection, more like a wrapper for the ArrayList class. Which doesn't have to be a problem but, in fact rajaloysious can still be a good solution too, though it is a bit more work.

HTH,

Razzie
0
 

Author Comment

by:Kokas79
ID: 13828935
Razzie: i can see that the struct implements the IEnumerable interface, which is good. Now..i dont want to use any array or arraylist or any other built in collection or dictionary....so instead of returning the arraylist's enumerator..can I create a new class inside the struct and have that implement the IEnumerator interface.
So in that class i can override all the interface's members...that is my goal.
i guess the enumerator method in my struct would be sth like this:

public IEnumerator GetEnumerator
{
  return new someClass(this);
}

and someClass should be sth like this(?):

class someClass : IEnumerator
{
  public someClass(myStruct str)
 { .... }

 .....

}
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 13831352
Well you can do that, but having a class IEnumerator in itself doesn't make it a Collection or something. If you inherit from IEnumerator, it means that it gets these members:

Reset()
MoveNext()
Current

So this still means that you need to implement these methods, so I'd say you'd still need some collection class (unless you want to do something totally different with it). If the MoveNext() method is 'empty' your foreach() statement would be useless.
0
 

Author Comment

by:Kokas79
ID: 13831375
So by implementing IEnumerator and overriding those member...this still is not enough to make my class a custom collection?
Does a class have to implement both interfaces...enumerable and enumerator...to become a collection?

Where i'm having difficultis now is overriding the Current property that returns an object...i dont know how to return it.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 13831728
That is still not enough. In fact, it is very hard to create your own collection without inheriting from CollectionBase. That's why I said rajaloysious's solution was still good.

So unless you can program a solution that stores data as an array without using an actual built-in array class, it is not possible.

So that leaves the obvious question, why do you want to write your own array class? :)
0
 

Author Comment

by:Kokas79
ID: 13831738
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 13831891
Yes, you can do that, that would work great. Notice that even that class uses an array to store the actual objects. Only my example earlier was with an ArrayList. But this way you inherit both Interfaces and not use CollectionBase so if that's what you want, I would go this way.
0
 

Author Comment

by:Kokas79
ID: 13831910
Yes that was the bit i could not understand...why the class uses the array to store the book objects...i thought by implementing those interfaces u dont need to do that.
I will probably replace that array with arraylist to be more flexible..thanks.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question