Here are the classes that i've attempt to create as well as tried to use them but failed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.GlobalVariables
{
    static class IOCard
    {
        public const int TotalInputCard = 10;
        public const int TotalOutputCard = 10;

        public  class InputCard
        {
            public string CardNo = "1";
            public int BaseAddress;
            public int LowerAddress;
            public int UpperAddress;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        public class OutputCard
        {
            public string CardNo;
            public int BaseAddress;
            public int LowerAddress;
            public int UpperAddress;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        public static InputCard[] InputCards = new InputCard[TotalInputCard];
        public static OutputCard[] OutputCards = new OutputCard[TotalOutputCard];

        public static int X100 = InputCards[0].WriteBitNo[0];
        public static int Y100 = OutputCards[0].WriteBitNo[0];
    }
}

And I tried to use it in the Form_Load like so

private void Form1_Load(object sender, EventArgs e)
{
    IOCard.X100 = 1;
    IOCard.Y100 = 1;
}

However, i get the error attached below.
Please advice.
Thanks.

Lines 33 and 34 declare space for the arrays, but don't actually allocate objects into the space, so when you try to use them in lines 36 and 37 there is nothing there to access. You need to actually declare the objects (not just the array to hold them). You'll need a static constructor.

Edited 5 Years Ago by Momerath: n/a

Thanks. Btw, where should I put the static constructor?
Inside IOCard class or InputCard class?
I did read about static contructor but i do not know how to use it here.

Sorry, I'm really stuck..

ok, in my class, i have a static constructor like so:

static IOCard()
{

}

but what do I put inside the constructor? How do I actually allocate objects into the space as per what was said in the post above?

Can somebody please point me to a sample code so that I can read and understand?
Thanks.

I tried to put the following code in but it didn't work..

static IOCard()
        {
            InputCards[0] = new InputCard();

            for (int j = 0; j < TotalInputCard; j++)
            {
                IOCard.InputCards[j].CardNo = "";
                IOCard.InputCards[j].LowerAddress = 0;
                IOCard.InputCards[j].UpperAddress = 0;

                for (int i = 0; i < 16; i++)
                {
                    IOCard.InputCards[j].WriteBitNo[i] = 0;
                    IOCard.InputCards[j].ReadBitNo[i] = 0;
                }
            }
        }

And nop, i don't really know what i'm doing or what i need to type..
hmm..

static IOCard() {
    for (int i = 0; i < IOCard.InputCards.Length; i++) {
        IOCard.InputCards[i] = new InputCard();
    }
}

This will create an object for each element of the array.

Hi, below is the updated class with the static constructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.GlobalVariables
{
    static class IOCard
    {
        public const int TotalInputCard = 10;
        public const int TotalOutputCard = 10;
        public static InputCard[] InputCards = new InputCard[TotalInputCard];
        public static OutputCard[] OutputCards = new OutputCard[TotalOutputCard];

        public class InputCard
        {
            public string CardNo = "1";
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        public class OutputCard
        {
            public string CardNo;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        static IOCard()
        {
            for (int j = 0; j < TotalInputCard; j++)
            {
               IOCard.InputCards[j] = new InputCard();
            }
        }
        public static int X100 = InputCards[0].WriteBitNo[0];
        public static int Y100 = OutputCards[0].WriteBitNo[0];
    }
}

And I use the class as shown below.

private void Form1_Load(object sender, EventArgs e)
{
    IOCard.X100 = 1;
}

However, I still face the error "Use the "new" keyword to create an object instance.".

When I debug the program and step through it, it tried to step through the code public static int X100 = InputCards[0].WriteBitNo[0]; in the class before running the static constructor. After that, the error above appear.

How can I solve it..?
Thanks.

static variables are initialized before the static constructor, so your last few lines cause an error. Move the initialization of the variable into the static constructor (and make sure you also initialize the OutputCards array just like you did the InputCards.)

This article has been dead for over six months. Start a new discussion instead.