i want to use a struct in a dictionary its key is a string and the value is the struct. i don't know what's the syntax as i'm new to c# i was writing c++ before. BTW i want to read from a text file and put the lines in the dictionary. here's what i did already:
public class CDinfo
{
public string code;
public string type;
public int count;
}
private void button1_Click(object sender, EventArgs e)
{
string pathsource = @"F:\Computer Science\CD & Instruments\CDs.txt";
CDinfo lolo = new CDinfo();
string name;
name = textBox1.Text;
lolo.type = textBox2.Text;
lolo.code = textBox3.Text;
lolo.count = int.Parse(count.Text);
Dictionary<string, CDinfo> MS = new Dictionary<string, CDinfo>();
StreamReader reader = new StreamReader(pathsource);
while (reader.Peek() != -1)
{
string m;
string[] fasl;
m = reader.ReadLine();
fasl = m.Split(',');
lolo.type = fasl[1];
lolo.code = fasl[2];
lolo.count = fasl[3];
MS.Add(fasl[0], lolo);
}
reader.Close();
StreamWriter sw = new StreamWriter(pathsource, append: true);
string s = name + ',' + lolo.type + ',' + lolo.code + ',' + lolo.count;
sw.WriteLine(s);
sw.Close();
MessageBox.Show("CD Added Successfully.");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
After running the solution i'm getting this error "Index was outside the bounds of the array".