Multi-dimensional arrays in an object.

I have some information in an object from a class that I have created. I only need one instance of this class, but the properties in it are reset several times during its use. The object is populated from one form and read from another so it needs to be global.

Most of the data in this object are multi-dimensional by nature so I have this info stored in multi-dimensional arrays. My problem is that the boundaries of the arrays often changes when the data in the object are reset. C# does not let me resize these arrays easily.

I am using Visual Studio 2008 / dot NET 3.5 and it is a C# Windows Forms Application.

I am not an expert in C# so I need to know if there is an acceptable way to reset the arrays whenever needed or do I need to dispose my object and create a new each time? What type of class definition would you recommend?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Do you need arrays? Have you considered using Lists instead?
daghoffAuthor Commented:
I have considered lists since I do know that arrays are not a C# programmer’s first choice, but from what I know lists are not ideal to store multidimensional info. Is that incorrect? I do not have a lot of experience with lists either.
anarki_jimbelSenior DeveloperCommented:
I also prefer to use lists. Or Lists of lists, but in some cases it may be not acceptable as list of list would be some analogue of jugged arrays.

So you may need to stay with multi-dimensional arrays. And yes, the easiest way to "reset" is to create a new array. The old one will be disposed automatically, you do not need to worry about this.
Some MSDN info on multidimensional arrays:
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

käµfm³d 👽Commented:
I don't immediately see why. I mean, depending on what you're doing with the data, searching and/or sorting could be complicated, but perhaps one of the other collections may be more well-suited.

Since Lists are generic, you can have lists of lists, lists of arrays, lists of dictionaries, etc. A list would be the closest structure to an array, you would just have more flexibility in adding elements. (Granted, resizing a list internally means that you resize the internal array buffer, but that's all masked from your view.)
anarki_jimbelSenior DeveloperCommented:
The problem is that we do not know initial requirements. For example: do we need to resize our two(multi)-dimensional structure on the fly.

If this is required list of lists would be the best option. I'd write a special, e.g., TwoDimensionalList class where I'd instantiate all lists and the list of lists to required size, would have utility methods to access list items, etc.

If we do not need to resize our structure on the fly - I would not bother with all this (too much complexity and overhead for almost nothing) and would use two-dimensional array disposing it when reinitializing my structure.

Something like that.
daghoffAuthor Commented:
Thanks for your input. I am looking into using Dictionaries lists now. The problem is that I have never properly learned how to use them. I always stick to arrays since I often work with VB. Now that I am moving to C# I need to stop using them.

Some of my arrays have 4 dimensions. Can you give me an example on how you would declare a four dimensional structure as a “list of list”?
käµfm³d 👽Commented:
public class SomeClass
    public string SomeProperty { get; set; }

Open in new window

List<List<SomeClass>> listOfLists = new List<List<SomeClass>>();

listOfLists.Add(new List<SomeClass>());
listOfLists.Add(new List<SomeClass>());
listOfLists.Add(new List<SomeClass>());
listOfLists.Add(new List<SomeClass>());

listOfLists[0].Add(new SomeClass());
listOfLists[1].Add(new SomeClass());
listOfLists[1].Add(new SomeClass());
listOfLists[2].Add(new SomeClass());
listOfLists[3].Add(new SomeClass());
listOfLists[4].Add(new SomeClass());  // Error

listOfLists[0][0].SomeProperty = "some value";

Open in new window

käµfm³d 👽Commented:
The outer list in the above is your first dimension; the inner lists are your second dimension. Since you have a list of lists, you can index them the same way you would a two-dimensional array.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.