CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2005
    Posts
    102

    class in dictionary

    Hey, i want to store multiple instances of a class inside a dictionary. I believe this is the way to do such a thing:

    private Dictionary<int, aClass> childs = new Dictionary<int, aClass>();

    Where 'aClass' is another .cs file in my project.
    But what if i don't know what the classname is?? Is there still a way to store them in the dictionary??

    I mean, i have several classfiles in my project. 'aClass', 'bClass' ... till 'zClass'.
    It depends on what the users does. I want to add a class to the dictionary by the actions of a user.

    So the first 2 items in my dictionary could 'aClass, aClass' and the 3rd 'lClass'.
    Is it possible like that, to store different classes in a dictionary?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: class in dictionary

    You may want to use an interface here that all of these classes implement. Then you simply create your dictionary like this and you can store any object that implements said dictionary as the value:

    Code:
    var dict = new Dictionary<int, IMyInterface>( );
    However, that will only be an option if it actually makes sense for these classes to implement the same interface. You don't give us any context here, so it is hard to help.

  3. #3
    Join Date
    Nov 2005
    Posts
    102

    Re: class in dictionary

    Well it's like this.
    I'm trying to make a custom GUI class. So for instance i have these classes:

    Panel.cs
    Button.cs
    Label.cs
    Image.cs

    Now a panel would be the first thing i want to create, so i'd do something like this:

    Panel.Init ( x, y, widht, height );

    This creates a custom made panel for me on the screen. But i also want to add buttons to that panel, but i have to tell that panel which belongs belongs to it. So that's why i'm planning to keep a dictionary of that. Cause in can be buttons, be it can also be Labels or Images something else.

    So in order to add a button to that panel i'd do something like this:

    Button1.Init ( x, y, widht, height );
    Panel.AddChild ( Button1 );

    That way i can have all the information (visibility, size, location etc) of Button1. Then i can also determine which buttons, labels i want to hide if the user chooses to hide the main panel.

    See what i'm trying here?

    So that's why i need a way to store classes inside a dictionary. But in that dictionary it could be buttons + labels + images etc.
    If the user wants to hide the panel i just loop through that dictionary to see what other controls i need to hide.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: class in dictionary

    OK, well all of those components inherit from Control, so just make maintain Dictionary of <int,Control>

  5. #5
    Join Date
    Nov 2005
    Posts
    102

    Re: class in dictionary

    actually im not using the default controls but im using images that look like controls. thats why im making a class for each control.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: class in dictionary

    OK, but what I am getting at is that they should all inherit from a common base class or implement a common interface.

  7. #7
    Join Date
    Nov 2005
    Posts
    102

    Re: class in dictionary

    Quote Originally Posted by BigEd781 View Post
    OK, but what I am getting at is that they should all inherit from a common base class or implement a common interface.


    yep that true. the main class, panel.cs should be able to inherrit multiple instances of any other gui class of mine

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: class in dictionary

    Quote Originally Posted by vivendi View Post
    yep that true. the main class, panel.cs should be able to inherrit multiple instances of any other gui class of mine
    I'm going to sound picky here, but you don't want to refer to a class as xxxxx.cs. That's just the .cs file that contains one or more classes. Strickly speaking foo.cs doesn't necessarily equate to the class 'foo'.

    Now on to storing classes in a dictionary. As bigEd as already mentioned you have two choices: make all your classes inherit from a base class or implement a common interface. Then store the base class or interface as the value of the dictionary.

    For example:

    Code:
     
    class Control { }
     
    class Panel : Control { }
    
    class Button : Panel { }
     
    private Dictionary< int, Control > _controlMap = new Dictionary< int, Control > ( );
     
    Panel p = new Panel( );
    Button b  = new Button( );
     
    _controlMap.Add( 1, p );
    _controlMap.Add( 2, b );
    Of course, you'll probally want to implement some polymorphism via overridden methods so you can cycle through the dictionary and call methods without worrying about the type of object you are calling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured