Set a STRING into DICTIONARY

Hi

I need to set a String into a Dictionary, but I have a error message.

string one = "One";
string two = "Two";

void Start()
{
Dictionary<string, string> myDictionary = new Dictionary<string, string> ();

myDictionary[one, two];

Debug.Log(myDictionary[one]);
}

Thank You

You need to add your entry to the Dictionary first.

myDictionary.Add(one, two);

Beware you can’t add an entry with a key that already exists in the Dictionary.
If you want to check if it contains the key use:

myDictionary.ContainsKey(one);

I just ADD my entry to the Dictionary, but I have that message:

Assets/Scripts/Store/StoreScript.cs(119,8): error CS0019: Operator `==' cannot be applied to operands of type `string' and `bool'

My code:

public string myString = "Two";
public string myOne = "One";
public string myTwo = "Two";

Dictionary<string, string> myDictionary = new Dictionary<string, string> ();

myDictionary.Add[myOne, myTwo];

void Start()
{
  if(myString == myDictionary.ContainsKey(myOne))
  {
    Debug.Log("OK");
  }
  else
  {
    Debug.Log("Fail");
  }
}

Thank You

ContainsKey returns a bool [True,False] to indicate whether the key exists in the Dictionary.
You’re getting the error because myString is a string and you’re trying to compare it against the bool type returned by ContainsKey.

What exactly are you trying to achieve here?

I need check if the value exist into myDistionary. Please, Look my code.

Please, listen.

if(myString == myDictionary.ContainsKey(myOne))

Does not work. You are telling it to compare a string to a boolean.

if(myDictionary.ContainsKey(myOne))

You only need this, but all it does is confirm that the list actually has a string that == myOne.

Further, you don’t seem to actually know how to code at all. You’re placing code out of scope and have very poor syntax comprehension. I’d suggest starting here before you get overwhelmed.

1 Like

I’m still not sure what you’re trying to achieve, but this code will add the key and value to the Dictionary.
Then check that that key is in the Dictionary, and compare the value corresponding to that key with your string value.

Again, I’m not sure what you trying to do, because of course the key and value will be in the Dictionary, you just added them.

As @LaneFox mentioned, all code that is not a declaration must be inside a method.
You had your code to add entries to the Dictionary outside of the Start method, this isn’t allowed.

public string myString = "Two";
public string myOne = "One";
public string myTwo = "Two";

Dictionary<string, string> myDictionary = new Dictionary<string, string> ();

void Start()
{
    myDictionary.Add(myOne, myTwo);
    if(myDictionary.ContainsKey(myOne) == true)
    {
        if (myDictionary[myOne] == myTwo)
        {
            Debug.Log("OK");
        }
        else
        {
            Debug.Log("myDictionary contains " + myDictionary[myOne]);
        }
    }
    else
    {
        Debug.Log(myOne + " does not exist in myDictionary");
    }
}

great, but I have a new error message:

Assets/Scripts/Store/StoreScript.cs(125,21): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Code:

if (myDictionary[myOne] != myTwo)
{
  myDictionary.Add[myOne, MyTwo];
}
else
{
  Debug.Log("OK");
}

The error is here:

myDictionary.Add[myOne, MyTwo];

Thank You

If you look at the matching line in the code I posted (line 9). You’ll see that you need to use rounded brackets on a method call, not square brackets.

iis it possible update a existing value into Dictionary ?

For example:

myDictionary.Add("Enemy", "A");

update to

myDictionary.Add("Enemy", "B");

Thank You

myDictionary["Enemy"] = "B";

Using Google would be better for you, because you won’t have to wait so long for an answer. Just copying your question into google gave me answer to your question.

Circling back to the first post… you need to understand how arrays work. And also understand the difference between an array and a function.

Array:
myDictionary[one] = two;

Function:
myDictionary.Add(one, two);

ok, but How could I get all values from myDictionary?

for (int i = 0; i < myDictionary.Count; i++)
{
  //Show results here
}

Thank You

Examples of dictionary use here:

Great site for recipes to common things with C#.

Great site, but I can’t see a topic about get ALL values from a Dictionary. Sorry

Thank You

That would be the loop example.

yes, thank you

           Dictionary<string, string> pet = new Dictionary<string,string>
           {
               { "Type", "Cat" },
               { "Name", "Purrly" },
               { "Sex", "Female" },
               { "Age", "5" }
           };

           // Print all values
           foreach (var value in pet.Values)
           {
               Debug.Log("Value: " + value);
           }

           // Print all keys
           foreach (var key in pet.Keys)
           {
               Debug.Log("Key: " + key);
           }

           // Print all key=value pairs
           foreach (var kvp in pet)
           {
               Debug.Log(kvp.Key + " = " + kvp.Value);
           }

Thank You Everybody, for help me. I aprecciate all replies. :slight_smile: