#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Help with Visual C# structures


    Hi

    I'm using Microsoft Visual C# 2010, and I'm very new to this (although I've had lots of previous experience in other languages).

    I may be going around this the complete wrong way, so any advice would be much appreciated.

    I'm trying to design a watering system computer: It is made up of different systems, each system comprised of 14 days, and each day comprised of 10 cycles, and each cycle has the properties hour, minute and duration.

    I thought the best way to go about working with all this data would be to define structures for a system, day and cycle. At the moment I have this:

    Code:
    namespace Water_System_Computer
    {
        struct WaterSystem
        {
            public WaterDay[] WaterDays;
    
            public WaterSystem(byte SystemIndex)
            {
    
                WaterDays = new WaterDay[14];
    
            }
            
        }
    
        struct WaterDay
        {
            public WaterCycle[] WaterCycles;
    
            public WaterDay(byte DayIndex = 0)
            {
                WaterCycles = new WaterCycle[10];
            }
    
        }
    
        struct WaterCycle
        {
            public byte hour;
            public byte minute;
            public byte duration;
    
            public WaterCycle(byte Duration = 0)
            {
    
                hour = 0;
                minute = 0;
                duration = 0;
    
            }
        }
    }
    In my main class I then have:

    Code:
     public partial class MainWindow : Form
        {
    
            private WaterSystem[] WaterSystems = new WaterSystem[16];
    
    ...
    In a particular method I then want to set some dummy data into a water cycle, and I have:

    Code:
    WaterSystems[0].WaterDays[0].WaterCycles[0].hour = 12;
                WaterSystems[0].WaterDays[0].WaterCycles[0].minute = 37;
                WaterSystems[0].WaterDays[0].WaterCycles[0].duration = 20;
    But this is where I'm getting errors. I'm guessing I've probably gone wrong fundamentally somewhere, but the error I'm getting at this point is NullErrorException: Object reference not set to an instance of an object.

    And help or pointers would be greatly appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    You're not allocating enough memory:

    Code:
                WaterSystems[0].WaterDays = new WaterDay[16];
                WaterSystems[0].WaterDays[0].WaterCycles = new WaterCycle[16];
                WaterSystems[0].WaterDays[0].WaterCycles[0].hour = 12;
                WaterSystems[0].WaterDays[0].WaterCycles[0].minute = 37;
                WaterSystems[0].WaterDays[0].WaterCycles[0].duration = 20;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Thanks, that's sorted it out!

IMN logo majestic logo threadwatch logo seochat tools logo