-3

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".

  • 2
    So what's the problem? What about it isn't working? – Servy Dec 26 '17 at 14:58
  • `yourDictionary.Add(key, value)`. – Daniel A. White Dec 26 '17 at 14:58
  • 2
    Note that `CDInfo` shouldn't be a `struct`. It's mutable, and structs shouldn't be mutable, it's not representing a single value, it's bigger than a struct should be, there's just no reason for it to not be a `class`. – Servy Dec 26 '17 at 14:59
  • There is absolutely no difference with adding any other kind of value in your Dictionary. – Pac0 Dec 26 '17 at 14:59
  • Side notes: 1 [public fields are bad design](https://softwareengineering.stackexchange.com/questions/161303/is-it-bad-practice-to-use-public-fields) 2.[mutable value types are evil.](https://stackoverflow.com/a/441323/3094533) – Zohar Peled Dec 26 '17 at 15:06
  • please see the edit – Maher Farghaly Dec 26 '17 at 15:09
  • @MaherFarghaly You've edited in more code, but still haven't stated what your problem is or what about your solution isn't working. – Servy Dec 26 '17 at 15:13
  • all i want to do is fill the dictionary – Maher Farghaly Dec 26 '17 at 15:17
  • @MaherFarghaly And what problem are you having doing that? – Servy Dec 26 '17 at 15:29
  • @Servy please see the error – Maher Farghaly Dec 26 '17 at 15:40
  • @MaherFarghaly What did you find when you did research on that error message and how did what you find fail to help you solve your problem? – Servy Dec 26 '17 at 15:42
  • It seems likely that there are multiple problems with your code. Based on your question, it seems you should start with the `IndexOutOfRangeException` and work back from there. That will start with reducing the code to the minimal required to reproduce the problem, and to **debug** the problem. See marked duplicate for advice on how to do that. – Peter Duniho Dec 26 '17 at 18:13
  • @PeterDuniho Non of those answers helped me. – Maher Farghaly Dec 26 '17 at 18:27

1 Answers1

0

You are initializing a new dictionary every time you click Button1, and you are not keeping track of it, so you will never be able to use it from a different context. You need to defined your dictionary as a class variable, and add a value to it from inside your click method:

Dictionary<string, CDinfo> MS = new Dictionary<string, CDinfo>(); // initialize the dictionary somewhere outside of your click handler

private void button1_Click(object sender, EventArgs e)
{
    CDinfo lolo = new CDinfo();
    string name;
    name = textBox1.Text;
    lolo.type = textBox2.Text;
    lolo.code = textBox3.Text;
    lolo.count = int.Parse(count.Text);

    // add to the dictionary here
    MS.Add(name, lolo);
}

Also, as some of the comments have already mentioned, CDinfo should probably be a class, not a struct.

Dave Smash
  • 2,941
  • 1
  • 18
  • 38
  • i don't know how to use a class that's why i'm using struct – Maher Farghaly Dec 26 '17 at 15:09
  • 2
    @MaherFarghaly A `struct` is *much* more difficult to use correctly (and you are not using it correctly). If you don't know how to use classes, then learn, because you're going to cause yourself a world of hurt abusing structs like this. – Servy Dec 26 '17 at 15:12
  • 1
    @MaherFarghaly - in your case, if you change the word "struct" to "class", then your code will work just as it is. You can also add methods to classes, though - for instance, the code to read the file belongs in your CDInfo class. I would take the time to walk through some tutorials before you go too much further... – Dave Smash Dec 26 '17 at 15:23