Reputation: 25
I'm currently just 2 errors away from a working program. I'll post the line that contains error and the definition of its variable and finally the errors. Please tell me where i got wrong. Thank you.
private Dictionary<string, bool[]> letterData = new Dictionary<string, bool[]>();
public char[] mapNeurons()
{
char[] map = new char[this.letters.Items.Count];
for (int i = 0; i < map.Length; i++)
{
map[i] = '?';
}
for (int i = 0; i < this.letters.Items.Count; i++)
{
double[] input = new double[Form1.DOWNSAMPLE_HEIGHT * Form1.DOWNSAMPLE_WIDTH];
char ch = ((string)(this.letters.Items[i]))[0];
bool[] data = this.letterData[ch]; //line contains errors
for (int j = 0; j < input.Length; j++)
{
input[j] = data[j] ? 0.5 : -0.5;
}
int best = this.network.Winner(input);
map[best] = ch;
}
return map;
}
and here are the error
Error 1 The best overloaded method match for 'System.Collections.Generic.Dictionary.this[string]' has some invalid arguments
Error 2 Argument '1': cannot convert from 'char' to 'string'
I used Char instead of String because if using String it will return the whole line of the string loaded, i just wants to return the first letter of the string
for example: data = A1:000001100000000110000000111000000011100000001
if use string it will return the whole line, i just wants it to return 'A'
NEW IDENTIFIED PROBLEM: when i enter the data, i put the data into 'A1' key, which explain why im using string, so when it didnt find 'A1' because it is char, it just find 'A' thats why it returned '?', is it possible if i use string n just read the first character which is 'A' in the string line?
Upvotes: 1
Views: 4705
Reputation: 9680
To convert char to string, ToString()
can be called. This is the solution for the question.
But hafizhans, was working on some program, for which he had asked question, where he converted the Dictionary<char, bool[]>
to Dictionary<string, bool[]>
. Now he was storing the data from dictionary to text file something like this
A1:001110000111100001010100
Now while reading this, he only needed A1
part, so Split
function did the trick.
Upvotes: 1
Reputation: 7259
UPDATED
Try this:
if(this.letterData.ContainsKey(ch.ToString()))
bool[] data = this.letterData[ch.ToString()];
or
if(this.letterData.ContainsKey(ch.ToString()))
bool[] data = this.letterData[ch+""];
Upvotes: 1
Reputation: 43077
First of all, you need to add items to the dictionary with the Add() method.
Secondly, your dictionary's key is a string, so you need to use a string as the index ... you can get the string representation of a char by calling ToString()
on the char instance.
char ch = 'x';
private Dictionary<string, bool[]> letterData = new Dictionary<string, bool[]>();
letterData.Add(ch.ToString(), new []{true, false});
bool[] data = this.letterData[ch.ToString()];
Or change the index to char:
char ch = 'x';
private Dictionary<char, bool[]> letterData = new Dictionary<char, bool[]>();
letterData.Add(ch, new []{true, false});
bool[] data = this.letterData[ch];
Upvotes: 4
Reputation: 44931
How about changing to:
string sCh = ch.ToString();
bool[] data = null;
if (this.letterDate.ContainsKey(sCh)
{
data = this.letterData[sCh];
}
In addition, you may want consider changing the key on the dictionary to be Char instead.
Upvotes: 0
Reputation: 5325
Try the following:
char charString = 'a';
if (letterData.ContainsKey(charString.ToString()))
{
// Found
}
Upvotes: 0
Reputation: 20764
the variable ch
is most likely a char you have to use a string instead
Upvotes: 0