Managing Objects in C# MDX

Started by
2 comments, last by enigmatix 16 years, 11 months ago
Hi, I'm creating a 3D model/map editor with MDX (C#) and I was wondering what would be the best way to manage my objects. As it stands, I have classes for my primitives. Each have a vertex buffer, index buffer, position coords, etc. What I was hoping to do was create a static array for my main object class which all my other classes inherit, but there is no way of dynamically adding to an array in C#. I've been told the ArrayList can accomplish this, but perhaps there is a better way? I should also mention that the array stores the actual object in it. For example, if I said: D3DSphere mysphere = new D3DSphere(); Upon creation, mysphere would be stored into the static array. Perhaps this a bad way of doing it? Any thoughts or comments are appreciated. Thanks.
Advertisement
You can use the following for dynamic arrays:

List<object_type> array_name = new List<object_type>();

That's all it is is really - a list.

"object_type" is obviously the type of object you wish the array to contain, and "array_name" is whatever you want it to be.

Just declare this as you need. You can add to the array by using array_name.Add(item). There are some handy methods associated with the List class too.

It's found in the System.Collections.Generic namespace so don't forget to include it with "using"
Your post is a bit too vague or broad. If you could be more specific. I'll try to answer with what I think you're asking.

Anyways, there is one thing to watch out for in c# and that is boxing and unboxing. It can literally kill your performance if you're not careful. Boxing and un-boxing happen when you store a value type into a reference type and work with it to and from value -> reference -> value -> reference ... etc. Basically what happens is that it has to do a lot of extra work changing the object from a value type to a reference type and back and forth.

One solution is to use generics. In-fact it is probably the best solution in my opinion. There is some info about it here: Generics

And yes you're right arrays are fast but not the answer for a list that will grow or shrink at various times during your application's run-time. Typically lists are used.

A list is nothing more that an array management class. It will handing operations for an array like: Adding items, removing items, resizing the array and so on and so forth.

I actually created my own array management classes and called them PowerList objects. But it is the same as the built-in DotNet Collections classes. You can find them in the namespace "System.Collections" in the "mscorlib.dll" reference file.

If you're are going to create a list that needs be optimized for speed you should use the generic counterparts located in the namespace "System.Collections.Generic". In there you'll find the List<T> class.
That should do what you want.

Again, just be careful to not store value types in reference type lists/arrays and you should be fine.

HTH,

Devin
Thanks guys. I'll be sure to try these solutions out.

This topic is closed to new replies.

Advertisement