Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Class Constructor not getting called when creating array of class object

Class Constructor not getting called when creating array of class object

Scheduled Pinned Locked Moved C#
helpdata-structuresquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mutty
    wrote on last edited by
    #1

    I am trying to create an array of classes however when I try to assign values to the class variables I get an error: "Object reference not set to an instance of an object" Am I declaring my array wrong? public class Form1 : System.Windows.Forms.Form {    private System.Windows.Forms.Button btn_FillArray    ... //other controls    MyClass[] classArray = new MyClass[5]; } private void btn_FillArray_Click(object sender, System.Eventargs e) {    classArray[0].data = 5; } class MyClass {    public MyClass()    {      data = 1;    }    public int data; } Thank you for the help - mutty

    S 1 Reply Last reply
    0
    • M Mutty

      I am trying to create an array of classes however when I try to assign values to the class variables I get an error: "Object reference not set to an instance of an object" Am I declaring my array wrong? public class Form1 : System.Windows.Forms.Form {    private System.Windows.Forms.Button btn_FillArray    ... //other controls    MyClass[] classArray = new MyClass[5]; } private void btn_FillArray_Click(object sender, System.Eventargs e) {    classArray[0].data = 5; } class MyClass {    public MyClass()    {      data = 1;    }    public int data; } Thank you for the help - mutty

      S Offline
      S Offline
      Sean Michael Murphy
      wrote on last edited by
      #2

      Hi Each element of the array has to be "newed" up individually, after the array has been declared. The array delcaration just frees up space for the number of objects, it doesn't instantiate them.

      MyClass[] classArray = new MyClass[5];

      for (Int32 i = 0; i < classArray.Length; i++)
         classArray[i] = new MyClass();

      Hope that helped. Sean

      M 1 Reply Last reply
      0
      • S Sean Michael Murphy

        Hi Each element of the array has to be "newed" up individually, after the array has been declared. The array delcaration just frees up space for the number of objects, it doesn't instantiate them.

        MyClass[] classArray = new MyClass[5];

        for (Int32 i = 0; i < classArray.Length; i++)
           classArray[i] = new MyClass();

        Hope that helped. Sean

        M Offline
        M Offline
        Mutty
        wrote on last edited by
        #3

        Yes, this was exactly what I was looking for. Thank you! -mutty

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups