1

Say I have a class who's constructor takes its values from data read from a file, is it also possible to create an instance of that class with a name from a string. That way I can create an instance of a class for each file I open and the constructor can be automatically loaded.

//User chooses file to open
//Read ChocolateCake.txt values
string className = Path.GetFileName(path);
//code to remove .txt    

//Instance name is taken from className
Cake "ChocolateCake" = new Cake(ingredient1, ingredient2, etc);

How would I create an instance of Cake with the name of the file I have read?

3
  • 3
    the variable name shouldn't make a difference at all, what are you trying to do? This sounds like an XY problem Commented Feb 19, 2015 at 9:17
  • He did ask a question, without a question mark though! Commented Feb 19, 2015 at 9:18
  • 1
    Why do you create a variable className but use a string literal "ChocolateCake"? But even if you fix that, why does the variable name matter at all(Sayse's comment)? It sounds as if you want to store those cakes by name. Then use a Dictionary<string, Cake> as Richard has suggested in his answer. Commented Feb 19, 2015 at 9:21

3 Answers 3

4

No, you cannot do this. You could however use a generic Dictionary<string, Cake> and store your cakes in that...

var cakes = new Dictionary<string, Cake>();

This cakes variable will hold multiple objects of type Cake, each keyed by a unique string value.

Adding a cake is as simple as...

cakes.Add("ChocolateCake", new Cake(incredient1, incredient2, etc));

And you can then access your cakes by key value (e.g. class name in your example), thus:

cakes["ChocolateCake"];
Sign up to request clarification or add additional context in comments.

3 Comments

So I create a list of type Cake and add to it in the way you have shown?
@Mike159 No lists, just a dictionary.
I'm suggesting that you use a Dictionary object, as this will allow you to store each cake with a string value that you can use to subsequently get the cake.
4

You could use a dictionary,

var cakes = new Dictionary<string,Cake>();
cakes.Add(className, new Cake(ingredient1, ingredient2));

Comments

1

Sometimes when I see issues like this which require some sort of object responsible for creating an appropriate type (e.g. Cake in your instance), based on input (in your case string), yes a Dictionary is a good starting place, but I always feel this is an indicator to look into a more flexible approach, e.g. using the Factory pattern.

Factories build objects. They usually do this using some parameter(s) that help decide what type of concrete object to build. The return type of a factory is often some kind of abstraction, i.e. an interface or an abstract class and the factory builds a concrete implementation of the abstraction.

This means you can have lots of different types of Cake object, and the factory return the correct one based on whatever is defined in your input string. One of the important benefits is the caller doesn't need to know up front what concrete type is expected, it will operate with an interface type instead meaning you don't need to keep updating the caller's code when new "Cake" types are added.

It's worth reading about abstract class factories, this is a nice little introduction.

2 Comments

Interesting article. I suspect correctly designing my programs from the outset would help a lot.
Here is another useful link which explains the patterns further: stackoverflow.com/questions/5739611/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.