2

I'm having trouble with declaring 2-dimensional arrays in C#, populating them and then returning the array.

At the moment, I'm declaring the array like so:

private static string[,] _programData = new String[50,50];
    public string[,] ProgramData 
    { 
        get 
        { 
            return _programData; 
        } 
    }

_programData is showing the error 'cannot implicitly convert from type 'string[,] to string[][]'

I should point out that I am attempting to call ProgramData from another class like so:

for (serviceCount = 0; serviceCount <= ffm.Program.Length; serviceCount++)
            {
                Console.WriteLine("Program Number: " + ffm.Program[serviceCount].ToString());
                for (serviceDataCount = 0; serviceDataCount <= ffm.ProgramData.Length; serviceDataCount++)
                {
                    **Console.WriteLine("\t" + ffm.ProgramData[serviceCount, serviceDataCount].ToString());**
                }
            }

Error occurs on the bold line above with:

Object reference not set to an instance of an object.

I don't think there seems to be an issue with how I've declared the array, its just the type mismatch that I don't understand.

Regards

6
  • Is the error reported on the "return _programData;" line or the line that calls ProgramData? If it's the latter, my guess would be that there is no string concatenation for 2D arrays. Commented Aug 20, 2009 at 9:25
  • I have resolved the error on that line now, it was a small bit of syntax I overlooked. It does appear that there is string concatenation for 2D arrays afterall. Once one error goes by, along comes another. Commented Aug 20, 2009 at 9:41
  • It would really help if you highlighted the line you were actually having the error on. BTW you appear to also have bug in that serviceDataCount will count up to 2500 you are only expecting 50. The Length of a multi-dimensional array is product of its dimensions. Commented Aug 20, 2009 at 9:43
  • An off-topic note: if you want to have a readonly property (ProgramData), you can leave out the "set { }" completely. Commented Aug 20, 2009 at 9:48
  • @AnthonyWJones The previous error has now been resolved, but it has now thrown up another error at the line: Console.WriteLine("\t" + ffm.ProgramData[serviceCount, serviceDataCount].ToString()); With the error - Object reference not set to an instance of an object. (NullReferenceException) I am not too sure where I am going wrong. @Hans Kesting: Good point, I'll take that on board. Regards Commented Aug 20, 2009 at 9:56

2 Answers 2

2

Firstly, calling ffm.ProgramData.Length will return 2500 (50 * 50) as stated above, so you need to fix that count to ffmProgramData.GetLength(1) to return the size of the second dimension.

Secondly, the error you get "Object reference not set to an instance of an object" occurs because you're referencing an uninitialized part of the array. Ensure that the array is filled, or at the very minimum, full of empty strings (obviously you need to run this loop in the constructor changing the variable names where appropriate, because ProgramData is read only as you have it):

for(int fDim = 0; fDim < ffm.ProgramData.GetLength(0); fDim++)
    for(int sDim = 0; sDim < ffm.ProgramData.GetLength(1); sDim++)
        ffm.ProgramData[fDim, sDim] = "";

Thirdly, you don't need to call the ToString() method inside the loop. You're casting a string to a string.

Sign up to request clarification or add additional context in comments.

1 Comment

This has solved the issue. I get no error messages now but my 2d array doesn't seem to be populated by my data i.e. it is printing out blanks I will try and sort this out but many thanks for now.
1

programData is showing the error 'cannot implicitly convert from type 'string[,*] to string[][]'

No, it does not show any error. The code compiles just fine (with C# 3.5).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.