1

Hi everyone and thanks for your help! First I tell you the context. I have to do on a windows 8 universal app, a Drink Vending Machine Simulator. My classes are:

  1. Manager (who creates the machine and puts in the ingredients and the drinks),

  2. VendingMachine (who has the list of ingredients and drinks, checks if there is enough drinks left, etc.)

  3. an Ingredient class with name and amount,

  4. an abstract base class Drink with name, price, etc. and

  5. classes Tea, Coffee and Chocolate who inherit from Drink and have own list of ingredient instances.

Now, when I select a drink in the machine, I'm checking every ingredient from it's name, if there is an ingredient with the same name on the machine I'm checking if there is left and I'm taking one.

The thing is, it should be easy to add a new type of Drink, but (!) if the name of the ingredient is not exactly the same it will not work. So I wanted to do an enum, I did it into the machine like:

public enum IngredientsNames
{
  Coffee,
  Tea,
  Chocolate,
  Sugar,
  Milk
}

The ingredient class is:

class Ingredient
{
    public string name { get; private set; }
    public int amount;
    public Ingredient(string _name)
    {
        name = _name;
    }
}

But the problem is if I want to do in the vending machine class:

IngredientNames.Coffee = "Coffee" ;

It says

"The left hand side of an assignment must be a variable, proprety or indexer"

To be clear, I wanted that if someone opens a new class of type of drink, and does Ingredient.Coffee it gives him automatically the string of the name "Coffee", he doesn't have to give the name himself, to not have an error after. I hope I was clear. Thanks for your help I'm still beginning in this.

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
  • can you show the definition of the `Ingredient` class with its constructor too, the logic to parse the string "Coffee" into a member of the `Ingredients` enum would be in there. Whenever something is "not working", please include complete error message (compiler error or runtime exception info). – Cee McSharpface Oct 07 '17 at 12:36
  • Sorry i'm new in the website , i did some edit in the post from what you asked – Sacha Krief Oct 07 '17 at 12:44
  • Check if this is what you want to do? Give an enum name and a description. https://stackoverflow.com/a/46424653/7974050 – Amit Kumar Singh Oct 07 '17 at 13:42
  • or, https://www.codeproject.com/Articles/11130/String-Enumerations-in-C – Amit Kumar Singh Oct 07 '17 at 13:50
  • Its not working for me , i want to give to the user that will add a new type of drink , a little list that he can choose the ingredient he want and will return him the string that corresponds – Sacha Krief Oct 07 '17 at 13:51

1 Answers1

2

You can convert a string value to enum like this.

Ingredients ingredient = (Ingredients)Enum.Parse(typeof(Ingredients), "Coffee"); 

You can also check whether that exists in Enum like this.

Enum.IsDefined(typeof(Ingredients), "Coffee") 
Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
  • this will give a string to the enum Coffee , i want that if someone add a new type of drink , he can directly do ingredients.add(Machine.Ingredient.Coffe) , when the first " ingredients " it's a list of ingredients instances there is on the Drinks base class , and after in the () its the enum who automaticly add an instance of the class ingredients with the name "Coffe" .. i don't know if i m clear .. – Sacha Krief Oct 07 '17 at 12:59
  • If you want that you just create your class, and Enum should automatically include it, that will not happen by itself. Values of enums should be known at compile time. However, check this answer which allows to replicate this scenario of dynamic enums. https://stackoverflow.com/questions/725043/dynamic-enum-in-c-sharp – Amit Kumar Singh Oct 07 '17 at 13:04
  • To get names of all classes in one namespace into a dictionary, you can check https://stackoverflow.com/questions/79693/getting-all-types-in-a-namespace-via-reflection/79785 – Amit Kumar Singh Oct 07 '17 at 13:10
  • The enum will not change it's not dynamic , there is a list of ingredients i puted myself. I can put the enum it the Ingredient class , i just want that each choice in the enum , gives the instance of ingredient. Let says if he does Ingredient.Coffee it's the same like doing Ingredient coffee = new ingredient("Coffee") , the user just don't do it himself , he don't put what he wants. A list of possibilities of ingredients already known – Sacha Krief Oct 07 '17 at 13:13
  • I want to know how i can give a value like a string or an objet instance to a choice in an enum , that the thing , if i have the enum { choice1 , choice2 , choice3 } that the choice1 is a specific object , choice2 another object , etc etc , that's what i don t know how to do – Sacha Krief Oct 07 '17 at 13:15
  • You won't be able to do that in the same enum. You can use Factory Design Pattern, pass the enum string, get the ingredient object. But factory design pattern will be better. https://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Design-Patterns-Factories?term=Factory%20Design%20Pattern – Amit Kumar Singh Oct 07 '17 at 13:17
  • I can't do an enum , with the IngredientNames , with let says coffe , tea and chocolate , when you do IngredientNames.coffe it gives you the string "Coffe" , when you do IngredientNames.tea it give you "tea" etc ? with just an enum ? – Sacha Krief Oct 07 '17 at 13:21
  • In the example in the answer, if you do ingredient.ToString(), it will give you the string name. But what you need is object of class in memory for execution. That object can be fetched through factory design pattern on the basis of the string. – Amit Kumar Singh Oct 07 '17 at 13:22
  • I'm not sure we speak about the same .. i did some changes at the end of the post maybe it's better now .. i just want that if someone add a new class of a new drink who inherit from the base class Drink , when he add the ingredient he have a list of names already known , he doesn't put it himself. Thats it – Sacha Krief Oct 07 '17 at 13:32