0

I am storing a class "Tag" and a string "tag name" into a dictionary list. This is an example of something I am storing in that list:

Tag apply_t = new Tag("<apply>");
        tagteam.Add(apply_t, "apply");

I can check the line if what I am getting has the characters I want...


I am passing the content below into the function that my if statements further down are in

char[][] arr = File.ReadAllLines(path).Select(l => l.ToCharArray()).ToArray();
    string line1tag = "" + arr[0][1] + arr[0][2] + arr[0][3] + arr[0][4];
    ReturnTag(line1tag);

Onward...

    if ((line == "a" + "p" + "p" + "l" + "y") || (line == "apply"))
    {
        Debug.Log(line);
    }
    else
    {
        Debug.Log("trying my best");
    }

BUT I really need it to be more dynamic than this... I want to be able to say something like

     if (line == tagteam.ContainsKey())
    {

    }

Since I am storing a class and a string name in the dictionary, how do I check for that?

I was thinking something like a temp Tag but I didn't know how to make that work.

Any thoughts?

TL;DR: The goal is to be able to read the string attached to the line, then to compare it to the dictionary and find the match. My problem is that I have a class with a string name on it right now. It gives me the error "Cannot convert from string to parseML.Tag".

  • 1
    `(line == "a" + "p" + "p" + "l" + "y") || (line == "apply")` those two conditions are identical. What do you actually want them to do? – Sach Nov 22 '19 at 21:25
  • And it's not quite clear what you are trying to do here. Is your dictionary `Key` of type `Tag`? That doesn't sound like good design. – Sach Nov 22 '19 at 21:26
  • Your question is a bit unclear. Are you trying to search the dictionary for a part of the tag or the full tag? The former is not what one generally does with a dictionary, the later is possible if you implement the `GetHashCode` method on your Tag class. – PMF Nov 22 '19 at 21:26
  • I only did that because I was worried about it storing separate chars or if it was taking the whole thing as a string. It's totally unnecessary now though. ( was receiving errors about it before so it was just checking) Yes my dictionary key is of the type Tag. I thought it was a good idea so I could have a dictionary of Tags, where each Tag would have specific attributes to itself when it's created. Idk I just wanted to search the dictionary for the string, and since I'm storing the class as the dictionary key I don't know how to search the dictionary for the string that's in the class key. –  Nov 22 '19 at 21:35
  • Your description is still unclear. _What do you want your program to do?_ is the question that needs answering. What's the purpose of items you store in the `Value` field of your current dictionary? According to your example, it's `apply`; is this supposed to be a unique string to identify each `Tag` instance you store in the `Dictionary`? – Sach Nov 22 '19 at 21:39
  • I basically want to take in a string, then search the dictionary for it. The dictionary would have the key as a class, which would then make it possible for me to add more to the class so that I can have a specific Tag be unique. Those tags stored in the dictionary, once compared to the string, would be stored on a stack of operations. All this boils down to a stack of operations that I could pop characters off of and do things with. –  Nov 22 '19 at 21:40
  • For clarification on the class part, let me give an example... If I read in , this signifies the plus sign, or plus operation. The Tag class reads in anything inside <> brackets, and stores it as a tag. When I am creating that Plus Tag I will have unique operations to it. Like number of arguments, name, etc... all defined by the class. –  Nov 22 '19 at 21:43
  • You appear to have an incorrect understanding of `Dictionary`. The dictionary `Key` is unique, but in the case where type of `Key` is a user defined class, dictionary doesn't decide if it's unique or not by looking at the contents of class properties etc. It decides the uniqueness by the object reference, so even if you store two instances with identical property values, the dictionary will still think they're two different items. – Sach Nov 22 '19 at 21:46
  • 1
    It's still not clear what you are actually looking for, but the question *as written* seem to be regular "how to find item in a dictionary by partial key or value match" (which is similar to https://stackoverflow.com/questions/15445432/check-if-dictionary-contains-substring). If that's not what you are looking for please [edit] the question to clarify. – Alexei Levenkov Nov 22 '19 at 21:50
  • Yeah... I've never used dictionaries before. I wanted them for the ability to lookup a key, so I could have a giant database of tags that were possible with associated unique contents (Like plus operator, name, variables). Should I just use a regular list for that? –  Nov 22 '19 at 21:50
  • I'm sorry but your question is still unclear. If you edit this or post a new question clearly stating your _goal_ that might be more helpful. – Sach Nov 22 '19 at 21:52
  • @McKennaWilson are you just looking for `dictionary.Add("apply", apply_t)`? – Alexei Levenkov Nov 22 '19 at 21:53

1 Answers1

0

You can't make it work as you imagine, that is because storing a concrete class in a Dictionary actually just stores its reference. If you make a new class it will have a new reference.

I am not sure of your design goals here, but you could implement the IEquatable interface and override GetHashCode , which is what classes like Dictionary use to compare equality

The IEquatable interface defines the Equals method, which determines the equality of instances of the implementing type.

The IEquatable interface is used by generic collection objects such as Dictionary, List, and LinkedList when testing for equality in such methods as Contains, IndexOf, LastIndexOf, and Remove. It should be implemented for any object that might be stored in a generic collection.

However, on saying all that, maybe you are just better off storing the tag as a string, in the dictionary?

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141