zack
zack

Reputation: 7345

C# unassigned local variable error

I am new to C#. I am trying to complile the following program but it throws an error given at the end: I know I am making a silly mistake. Any help would be much appreciated:

static void Main(string[] args) {

        IntPtr hCannedMessages = CannedMessagesInit();

        using (StreamReader sr = new StreamReader(CANNED_MESSAGE_FILE))
        {
            String line, sub;
            all_integer_IDs[] myobjarray;// = new all_integer_IDs[10];
            for (int c = 0; c < 10; c++)
            {
                myobjarray[c] = new all_integer_IDs();

            }
                line = sr.ReadLine();
                Console.WriteLine(line);

                if (line.Length > 15)
                {
                     sub = line.Remove(line.IndexOf(' ', 2));
                     Console.WriteLine("{0} \n",sub);

    myobjarray[0].setvalues((int)sub[2], (int)sub[3], (int)sub[4], (int)sub[5]);

Console.WriteLine("{0}, {1}, {2}, {3}", myobjarray[0].m_messageID, myobjarray[0].m_messagetype, myobjarray[0].m_classID, myobjarray[0].m_categoryID); }

               Console.Read();
            sr.Close();
        }

    }
}

}

And the class is in Class1.cs file in the same project and is as follows:

public class all_integer_IDs {

    public all_integer_IDs() 
    {

        setvalues(0, 0, 0, 0);

    }

    ~all_integer_IDs()
    {
    }

    public void setvalues (int messageID, int messagetype, int classID, int categoryID)
    {
        this.m_messageID = messageID;
        this.m_messagetype = messagetype;
        this.m_classID = classID;
        this.m_categoryID = categoryID;
    }

     public int m_messageID;
     public int m_messagetype;
     public int m_classID;
     public int m_categoryID;

}

The error is as follows: Use of unassigned local variable 'myobjarray' at line 55 which is copied and pasted below: myobjarray[c] = new all_integer_IDs();

Thanks, Viren

Upvotes: 2

Views: 4948

Answers (4)

Samuel Carrijo
Samuel Carrijo

Reputation: 17919

You have not allocated space for myObjarray. You need to allocate it

Use:

all_integer_IDs[] myobjarray = new all_integer_IDs[10];
for (int c = 0; c < 10; c++)
{
    myobjarray[c] = new all_integer_IDs();
}

at line 55.

And please use PascalCase for class names (in your case, AllIntegerIDs). Other developers will thank you for that

--EDIT, my bad. Corrected the way to call it. Please try the following

Upvotes: 2

Frederik Gheysels
Frederik Gheysels

Reputation: 56934

You never initialize myobjarray. You declare myobjarray, but you do not assign any memory to it; as in: you do not initialize the variable. You do initialize the elements of the array however (with yet another array), but you did not reserver any memory for the myobjarray itself.
(The initialization is commented out)

Upvotes: 0

Neil N
Neil N

Reputation: 25258

You never instantiated your array, it seems as though you commented out that part.

If you want a variable length array, try a list<> instead.

Upvotes: 0

Matthew Jones
Matthew Jones

Reputation: 26190

It looks like you need to declare the size and type of the array myobjarray when you instantiate it. In fact, it looks like you've already got that code, you just need to remove the comment symbol.

all_integer_IDs[] myobjarray = new all_integer_IDs[10]();

Upvotes: 1

Related Questions