Click to See Complete Forum and Search --> : class in dictionary
vivendi
October 16th, 2009, 01:41 PM
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?
BigEd781
October 16th, 2009, 01:46 PM
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:
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.
vivendi
October 16th, 2009, 02:32 PM
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.
BigEd781
October 16th, 2009, 03:01 PM
OK, well all of those components inherit from Control, so just make maintain Dictionary of <int,Control>
vivendi
October 16th, 2009, 04:11 PM
actually im not using the default controls but im using images that look like controls. thats why im making a class for each control.
BigEd781
October 16th, 2009, 04:13 PM
OK, but what I am getting at is that they should all inherit from a common base class or implement a common interface.
vivendi
October 16th, 2009, 04:52 PM
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
Arjay
October 16th, 2009, 08:29 PM
yep that true. the main class, panel.cs should be able to inherrit multiple instances of any other gui class of mineI'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:
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.