Resolved Can you use a varable to find a value in a Dictionary?

Alivegamer

Member
Joined
Jul 20, 2022
Messages
10
Programming Experience
1-3
I've been looking for a answer for this question for a bit and i can't find the answer.
I want to make a variable that has a string value store in it. That connects to a string value to a dictionary and get the int value of the dictionary.
C#:
Static IDictionary<string, int> Dict = new Dictionary<string, int>
{
    {"Value 1", 0},
    {"Value 2", 1}
};

string Variable = "Value 1";

if (Dict[Vaiirable] == 0)
{
    Console.WriteLine("Equal");
};
^^ hopfully this can help you understand what I'm trying to ask if I can't type it right. Hopfully I'm not over thinking this.
 
So what's the problem? Variables aren't magic. They simply store a value. When you use a value, it doesn't matter whether you use a literal, a variable or a complex expression. The result is still the same. That code will obviously not work as it is because the first part can't be in a method and the last part can't be outside a method but if you put all the parts in the right place then it will work as you seem to want. I ask again, what's the problem? If the code doesn't do what you want it to, maybe you could explain what it does do and how that differs from what you want.
 
So what's the problem? Variables aren't magic. They simply store a value. When you use a value, it doesn't matter whether you use a literal, a variable or a complex expression. The result is still the same. That code will obviously not work as it is because the first part can't be in a method and the last part can't be outside a method but if you put all the parts in the right place then it will work as you seem to want. I ask again, what's the problem? If the code doesn't do what you want it to, maybe you could explain what it does do and how that differs from what you want.
omg im so stupid nevermind i just had the if statement wrong. Had = not ==.
 
Back
Top Bottom