locked
Creating a custom class with properties of arraylist. RRS feed

  • Question

  • Hello, I've been trying to make a good class to use in a DirectX game I am making but I'm having trouble with some code. Is is possible to make a class that has an arraylist of objects with properties that can be accessed with the . ?

    Say I want to access the rotation property of the 5th object in my class how could I make my code look like this:

    scene.mesh[5].rotation;



    I've seen it done in System.Xml where you access nodes in the xml tree. (xmldocument.DocumentElement.ChildNode[x].NodeProperty;).


    I guess it doesn't have to be an arraylist. It just has to be an array that you can change the size of dynamically? Is that possible

    Thanks a lot! Sorry if I don't make sense I'm sort of new to C#.


    Ryan
    • Edited by Ranaakamarth Thursday, July 3, 2008 1:53 AM changed my question kinda
    Thursday, July 3, 2008 12:42 AM

Answers

  • I would implement the object classes as derived from a base class that offers all or most of the  properties you would normally access.  For instance, call the base class...

        public abstract SceneObjectClass
        {
            // define your common properties and methods
        }

    Derive all your scene objects from this class.  Then declare a list like...

        List<SceneObjectClass> sceneObjectList = new List<SceneObjectClass>();

    Add your objects to the list with...

        sceneObjectList.Add(new DerivedFromSceneObjectClass());

    You can access the list with indexes...

        currentObj = sceneObjectList[currentIndex];

    ... and you can access the properties, methods, and events of your base class (presumably overridden by your derived classes...

        sceneObjectList[i].mesh = MyMesh.WhatEver;
        sceneObjectList[i].material = MyMaterial.Leather;
        //etc.

    And, for those classes that have special members a that make no sense in the base class, you can still cast...

        ((MySpecialSceneObjectClass)sceneObjectList[i]).ToothEnamelColor = MyColors.SlimyGreen;

    Last, if you need a true array of objects sometimes, you can use the List<T>.ToArray() method to get an array of SceneObjectClass objects.

        SceneObjectClass[] myArray = sceneObjectList.ToArray();

    Template types and generic lists are extremely useful in these applications.
    • Proposed as answer by Ranaakamarth Thursday, July 3, 2008 3:41 PM
    • Marked as answer by Ranaakamarth Thursday, July 3, 2008 3:41 PM
    Thursday, July 3, 2008 2:14 AM

All replies

  • I may be misfiring but still it is tempting to try. I will start with explaining my own understanding of what you are trying to do.

    You've got a set of goofy objects: object1, object2, object3, etc. They ALL have potentially different types, in other words they are implementations of different classes. You want do have an instrument of accessing the elements of your ArrayList in a type-safe manner in other words to access them in such a way as to not get an exception being thrown. Am I correct?

    I guess this will suffice:

    ArrayList arrL = new ArrayList ( );

    ............ // arrL is filled.

    ( arrL[jj].GetType ( ) ) arrL[jj]; will give you the typed element jj or your arrayList or:

    ( arrL[jj].GetType ( ) ) localElement = (arrL[jj].GetType ( ))arrL[jj];

    You can use your localElement from now on.


    AlexB
    Thursday, July 3, 2008 1:29 AM
  • Thanks... but thats not exactly what I was trying to do. I'll post the entire scenario now.


    I'm making a class for my Direct3D application. There is a class i'm making called Scene. The Scene contains an arraylist of objects in the scene. The number of objects in the scene can change, thats why I need an arraylist. Then I need to be able to access the properties of each object in the scene. The objects properties would be stuff like mesh, material, transformation and such.


    Thursday, July 3, 2008 1:40 AM
  • I would implement the object classes as derived from a base class that offers all or most of the  properties you would normally access.  For instance, call the base class...

        public abstract SceneObjectClass
        {
            // define your common properties and methods
        }

    Derive all your scene objects from this class.  Then declare a list like...

        List<SceneObjectClass> sceneObjectList = new List<SceneObjectClass>();

    Add your objects to the list with...

        sceneObjectList.Add(new DerivedFromSceneObjectClass());

    You can access the list with indexes...

        currentObj = sceneObjectList[currentIndex];

    ... and you can access the properties, methods, and events of your base class (presumably overridden by your derived classes...

        sceneObjectList[i].mesh = MyMesh.WhatEver;
        sceneObjectList[i].material = MyMaterial.Leather;
        //etc.

    And, for those classes that have special members a that make no sense in the base class, you can still cast...

        ((MySpecialSceneObjectClass)sceneObjectList[i]).ToothEnamelColor = MyColors.SlimyGreen;

    Last, if you need a true array of objects sometimes, you can use the List<T>.ToArray() method to get an array of SceneObjectClass objects.

        SceneObjectClass[] myArray = sceneObjectList.ToArray();

    Template types and generic lists are extremely useful in these applications.
    • Proposed as answer by Ranaakamarth Thursday, July 3, 2008 3:41 PM
    • Marked as answer by Ranaakamarth Thursday, July 3, 2008 3:41 PM
    Thursday, July 3, 2008 2:14 AM
  • Ranaakamarth said:

    Thanks... but thats not exactly what I was trying to do. I'll post the entire scenario now.


    I'm making a class for my Direct3D application. There is a class i'm making called Scene. The Scene contains an arraylist of objects in the scene. The number of objects in the scene can change, thats why I need an arraylist. Then I need to be able to access the properties of each object in the scene. The objects properties would be stuff like mesh, material, transformation and such.


    What if instead of a class with an arrayList containing your properties you make a class with ALL your properties defined in a conventional, proper manner but in a particular instance of the class you simply set some of them to null (make them nullable) and that will be it. They simply will not be defined, that's it.

    Some of the properties may not be nullable (like strings) then make them empty as default.

    It seems to be a much easier solution.


    AlexB
    Thursday, July 3, 2008 12:16 PM