- Home /
Hashtable problem c#
I have a hashtable with A lot of words , when i introduce the words manually into the hashtable*[hashtable.add(id,"name")]it works perfectly with [hashtable.ContainsValue(InputField)]*, but somehow, when I introduce data in the hastable via automation, like
int i = 0;
foreach (string s in LinesDiccionaryD)
{
Tabla.Add(i, s.ToLower());
i++;
}
foreach (string s in LinesDiccionaryS)
{
Tabla.Add(i, s.ToLower());
i++;
}
it just stop working with [hashtable.ContainsValue(InputField)], how can i solve that, sorry for my english , and thx btw, I hope someone can help me
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
public class DiccionaryReader : MonoBehaviour
{
private string LastLine;
private string line;
public int currentLine = 0;
public int CurrentLine3;
public int CurrentLine4;
Hashtable Tabla;
private string[] lines;
private string[] LinesDiccionaryS;
public string[] LinesDiccionaryD;
private string content;
private string contentDiccionaryS;
private string contentDiccionaryD;
private string[] tokens;
public Text Palabra;
public Text Conjugacion;
public Text Tipo;
public Text Traduccion;
public bool SearchingForWord;
public bool UpdatingDiccionaries;
public bool UpdatingDiccionaryD;
public bool UpdatingDiccionaryS;
public InputField SearchWord;
public TextAsset file;
public TextAsset FileDiccionaryS;
public TextAsset FileDiccionaryD;
public void Start()
{
FileDiccionaryD = Resources.Load("Info/DiccionaryD") as TextAsset;
FileDiccionaryS = Resources.Load("Info/DiccionaryS") as TextAsset;
file = Resources.Load("Info/Diccionary") as TextAsset;
content = file.text;
contentDiccionaryD = FileDiccionaryD.text;
contentDiccionaryS = FileDiccionaryS.text;
lines = content.Split('\n');
LinesDiccionaryD = contentDiccionaryD.Split('\n');
LinesDiccionaryS = contentDiccionaryS.Split('\n');
Tabla = new Hashtable();
NewWord();
int i = 0;
foreach (string s in LinesDiccionaryD)
{
Tabla.Add(i, s.ToLower());
i++;
}
foreach (string s in LinesDiccionaryS)
{
Tabla.Add(i, s.ToLower());
i++;
}
foreach (var key in Tabla.Keys)
{
Debug.LogWarningFormat("{0}: {1}", key, Tabla[key]);
}
}
public void NewWord()
{
{
var r = new System.Random();
int randomNumber = r.Next(0, lines.Length);
// Read the random line
line = lines.Skip(randomNumber - 1).Take(1).First();
string Output = line;
LastLine = line;
string MainString = Output;
tokens = MainString.Split('&');
Palabra.text = tokens[0];
Tipo.text = tokens[1];
Conjugacion.text = tokens[2];
Traduccion.text = tokens[3];
}
}
public void Update()
{
if (Tipo.text == "V")
{
Tipo.text = "Verbo";
}
else if (Tipo.text == "S")
{
Tipo.text = "Sustantivo";
}
else if (Tipo.text == "O")
{
Tipo.text = "Oracion";
}
else if (Tipo.text == "A")
{
Tipo.text = "Otro";
}
}
public void SearchForWord()
{
Debug.Log(Tabla.Count);
Debug.Log(SearchWord.text);
if (Tabla.ContainsValue("ab"))
{
Debug.Log("la encontre");
}
else if (Tabla.ContainsKey(SearchWord.text))
{
var key = Tabla[SearchWord.text];
Debug.Log(key);
}
else
{
Debug.Log("Ni Fruta idea que estas buscando");
}
}
}
I would like to help but I'm afraid I'm a bit unclear about the question...
It is also incredibly difficult to read your question due to the current formatting. I recommend using muti-line code format for stuff like this:
//Code like this for long snippets
As opposed to using single line code like this.
Also, please only post relevant code and not entire scripts unless absolutely necessary. Finally, don't post an answer to amend your question, but ins$$anonymous$$d edit the question itself or use comments for side-notes.
I fear that in its current state your question will go un-answered. I recommend deleting and re-posting this question, using more readable formatting and providing more details about the actual problem. Stuff like this can go a long way to ensure a question gets answered and not forgotten.
Answer by Bunny83 · Jun 26, 2019 at 10:23 PM
Your code makes no sense. Your hashtable has integer values as keys. So you can not possibly find a string key in that hashtable. It's not really clear what that hashtable should be used for. A hashtable maps a key to another value. You just used an integer index as key and the actual string as value. If you want to be able to actually find the string values, you should have the string as key. However it's not clear to which value you want to map each word to.
If you just want a hash table to see if a word exists or not you should use a HashSet. The Hashtable class shouldn't really be used anyways. It's generally better to use the generic classes like the HashSet i just mentioned or if you actually need a mapping use a generic Dictionary. If you would have used a generic dictionary your code wouldn't even compile since you have to specify your key and value type. So adding integers but testing for strings can never work.
Your answer
Follow this Question
Related Questions
Hashtable.Enumerator: snapshot out of sync 1 Answer
Hashtable Bejeweled movement 1 Answer
Causing a chain reaction and hashtable errors 0 Answers
Fast nearest neighbour searches 0 Answers
Converting String to Variable Name 3 Answers