I need to add item to a dictionary while reading data from a text file. I want that for every line I will have x data from this particular line in my array. So I declare one two-dimensional array in my class:
Contactsp = new string[][] { };
int number = 0;
using the dictionary:
Dictionary<string, Contact> dict = new Dictionary<string, Contact>();
readin from the file:
foreach (var line in File.ReadAllLines(fileName).Where(l =!string.IsNullOrWhiteSpace(l)))
{
for (int j = 0; j < line.Length; j++)
{
for (int i = 0; i < line.Length; i += 9)
{
string hexText = line.Substring(i, 9);
string c = hexText.Substring((hexText.Length - 2));
int length = Convert.ToInt32(c, 16);
Char[] B = new char[length];
string key = line.Substring(i, 4);
string value = line.Substring(i + 9, length);
i += length;
if (!dict.TryGetValue(key, out myContact))
{
myContact = new Contact();
// didn't find a record for this key, so add a new one
dict.Add(key, myContact);
}
dict[key].Contactsp[j][dict[key].number++] = value;
}
j++;
}
}
while running it I am getting the exception of "System.IndexOutOfRangeException" on the line dict[key].Contactsp[j][dict[key].number++] = value;
Why?
>`
– Amit Jun 04 '18 at 04:35>?
– Monika Jun 04 '18 at 04:43