Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi. I need help to write a generic function.
public class MyClass
{
   public type1 obj1;
   public type2 obj2;

public MyClass (Dictionary<String, String> dico)
{
   // TODO...
   //Dico contains <"type1", "value1"> and <"type2", "value2) 
   //I want to assign automatically value1 in this.obj1 et value2 in this.obj2
}


I have many objects in my class. To build the dictionary I parsed a String like this "type1 = value1".

Think you for your help
Posted
Comments
johannesnestler 26-Jan-12 18:03pm    
Did you kind of "serialize" MyClass-Object(s) and try to rebuild them this way?

You are not even trying to write anything generic. Where you use "<>" syntax you do something just the opposite — instantiate generic class. The class System.Collections.Generic.Dictionary is generic, the class System.Collections.Generic.Dictionary<string,> is not.

You really need to learn syntax and the idea of generics.

In your case, you need something like:
C#
class MyClass<KEY, VALUE> { //by the way, think if you really need it public
   internal MyClass(Dictionary<KEY, VALUE> dictionary) {
      this.Dictionary = dictionary;
   }
   Dictionary<KEY, VALUE> Dictionary;
   //...
} //class MyClass

//usage:
Dictionary<KEY, VALUE> dictionary = new Dictionary<KEY, VALUE>();
MyClass<string, string> instance = new MyClass<string, string>(dictionary); 


Better yet, think about instantiating of the dictionary inside class. Is there any reason to use external dictionary?
Also, remember that your dictionary is of the reference type (a class). If you pass the same dictionary to two different instances of MyClass, adding an element in one instance will change the dictionary in another one because they reference the same Dictionary object. Do you really need that?

You can construct it where it is declared. You won't need a constructor with this parameter then.

So, if you change your mind, it will be:

C#
class MyClass<KEY, VALUE> { //by the way, think if you really need it public
   Dictionary<KEY, VALUE> Dictionary = new Dictionary<KEY, VALUE>();
   //some code using Dictionary
   //...
} //class MyClass

//usage:
MyClass<string, string> instance = new MyClass<string, string>(); 


—SA
 
Share this answer
 
v5
First off, this has nothing to do with generic functions: they are of the form
C#
public void MyFunction<T>(T myParameter) {}
Which your code isn't.

What you want to do is, I think, assign the value from a dictionary to a class level variable whose type is given as the dictionary key.

You can do that, with Reflection, but it a extremely nasty and prone to error: what do you do if there are two objects of type1 in the class? Or none?

Perhaps if you explain what you are trying to do that you think this is a good solution to, there may be a better solution.

BTW: Exposing fields as public is a bad idea - you should use a property (automatic if necessary) instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900