How To instanz an class with an array

jobonabas

New member
Joined
Aug 12, 2016
Messages
2
Programming Experience
1-3
C#:
public class Diagramm_Daten
    {
        public string error { get; set; }
        public List<Diagramm_Zahlen> zahlen { get; set; }
    }

public class Diagramm_Zahlen
    {
        public DateTime VON { get; set; }
        public DateTime BIS { get; set; }
        public Int32 Aufrufzahlen { get; set; }
    
    }

private void Erstellen_Click_1(object sender, EventArgs e)
{
   foreach (DataGridViewRow Row in dataGridView4.Rows)
   {
       Diagramm_Daten datensatz = new Diagramm_Daten();
       Diagramm_Zahlen datensatz_2 = new Diagramm_Zahlen();
  
       datensatz_2.VON = new DateTime(2100, 12, 31);
       datensatz_2.BIS = new DateTime(1900, 01, 01);
       datensatz_2.Aufrufzahlen = Convert.ToInt32(Row.Cells["count"].Value);

       datensatz.error = Row.Cells["error"].Value.ToString();
       datensatz.zahlen.Add(datensatz_2);

       Diagramm.Add(datensatz);
   }
}

Exeption null reference is thrown here: datensatz.zahlen.Add(datensatz_2);

I think its because the List "datensatz.zahlen" has not been declared as a new object but

C#:
Diagramm_Zahlen datensatz.zahlen = new Diagramm_Zahlen();
Diagramm_Zahlen[] datensatz.zahlen = new Diagramm_Zahlen[];
Diagramm_Zahlen datensatz.zahlen[] = new Diagramm_Zahlen();

is not working at all ...
someone here who knows how to solve it ?
 
There's a right way and a wrong way to define collection properties in a class. This is the wrong way:
public class Diagramm_Daten
    {
        public string error { get; set; }
        public List<Diagramm_Zahlen> zahlen { get; set; }
    }
This is the right way:
public class Diagramm_Daten
    {
        private List<Diagramm_Zahlen> _zahlen = new List<Diagramm_Zahlen>();

        public string error { get; set; }
        public List<Diagramm_Zahlen> zahlen { get { return _zahlen; } }
    }
That's the way it's done throughout the .NET Framework. The property is read-only so you can get the existing collection in order to call its methods, e.g. Add or Remove, but you cannot set it by assigning a completely new collection.

By the way, you're free to follow whatever conventions you want but it is very widely considered correct to start variable names, be they members, locals or parameters, with a lower-case letter and member names other than private fields with an upper-case letter. You actually seem to be using no convention at all as you have upper- and lower-case used in different places at different times. Decide what you're going to use and stick to it, otherwise you simply make your code more confusing. If you don't have a good reason not to stick to Microsoft's recommendations, stick to them.

EDIT: I wanted to point out that I have prefixed that field name with an underscore to distinguish it from the property because I just noticed that the bottom row of pixels on that line is not displayed on my system so you can't see it. I'm not sure whether the same will happen to you but now you know either way.
 
Thank you very much one of the best answers I got at any question so far,
I didn?t thought of conventions cause I am writing it only for myself and just wanted to finish as fast as possible cause I originaly wanted to do something else,
I will update it to follow these conventions when I am ready and have time for it, The code is not that long so that I have no Problem to read it.
 
Once you start using conventions, you keep using them without thinking about it. Consistency is always a good thing. It's up to you but I'd recommend making the changes now. VS can help you rename all instances of an identifier when you change the declaration so it's not especially onerous but it will only become more so if you leave it longer.
 
Back
Top Bottom