Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Iteration woes

Posted on 2005-04-28
3
Medium Priority
?
206 Views
Last Modified: 2010-04-16
I've got something like

class Foo
{
    public static readonly Foo Bar_1 = new Foo(1);
    public static readonly Foo Bar_2 = new Foo(2);
    public static readonly Foo Bar_3 = new Foo(3);

    private Foo(int i) {
        // ...
    }
}

Now I want to have a loop that goes: Bar_1 -> Bar_2 -> Bar_3.
To avoid a maintenance headache I want to be able to add a Bar_4, Bar_5 etc. without the necessity of modifying any other fields.

TIA
0
Comment
Question by:__alex
3 Comments
 
LVL 8

Accepted Solution

by:
Razzie_ earned 800 total points
ID: 13883925
Well if you would absolutely want to do it this way (it would be easier to store them in a collection so you could simply say 'foreach(Foo foo in Foo.Foos)' or something) you would have to use Reflection to accomplish this. It would be something like:

System.Reflection.MemberInfo[] members = myFoo.GetType().GetMembers(BindingFlags.Static | BindingFlags.Publc);

And now you could iterate over the members array:

foreach(MemberInfo member in members)
     Console.WriteLine(member.Name);

Haven't tested the code.

HTH,

Razzie
0
 
LVL 7

Assisted Solution

by:jj819430
jj819430 earned 200 total points
ID: 13896502
Razzie is right, don't do it this way unless it is neccessary.

do something like this for a basic version, or you can go in a build with the IEnumerable interface.

class Foo
{
private ArrayList _Items;
public ArrayList Items
  {
  get
  {
   return _Items;
  }
  set
  {
   _Items = value;
  }
  }
public Foo(int i)
{
 ArrayList _Items = new ArrayList();
}
}

now you can just access the items through the ArrayList (Foo.Items[Index])
if you want to do a foreach just inherit from IEnumerable and implement and iterator.
0
 
LVL 2

Author Comment

by:__alex
ID: 13907407
Well, actually I use an enum and an attribute
{
    [MyAttribute(1)]
    Bar_1,
    [MyAttribute(2)]
    Bar_2,
    ...
}
and evaluate the attribute at runtime but I feel this isn't the right approach.

There's no need for the client of my class to iterate over the bars but I need to search for a certain 'Bar':
Foo GetTheBarWithAttributeX(int x) {
  foreach(bar in ...) {
    if (x == bar.GetAttribute) return b;
  }
}

I think the best solution is to repeat myself:
class Foo
{
    public static readonly Foo Bar_1 = new Foo(1);
    public static readonly Foo Bar_2 = new Foo(2);
    public static readonly Foo Bar_3 = new Foo(3);

    private static readonly Foo[] bars = {Bar_1, Bar_2, ...}
    ...
}

Thank you!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

579 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