Forum Discussion

pmahesha's avatar
pmahesha
Occasional Contributor
13 years ago

Array of Class Objects

I would like to know how to create an array of class type.



I have defined ODT class named "TextFieldClass" with two properties property1 and property2

To create an object of this class, I use ODT.Classes.New(" TextFieldClass");

Then I initialize each class items.

Ex:

myObj=ODT.Classes.New(" TextFieldClass");

myObj.property1="My Name";

myObj.property2="My Address";



But I want to create an array of type TextFieldClass. I could not find a direct method. I used indirect method:

var myArrayObj= new Array();



// Create 5 objects of TextFieldClass type



for(var count=0;count<5;count++)

{

     myArrayObj[count]=ODT.Classes.New(" TextFieldClass");

  }

// Now initialize individual elements as required.

myArray[0].property1= "Name1";

myArray[0].property2' "Address1"

myArray[1].property1= "Name2";

myArray[1].property2' "Address2"



Though this worked fine, I want to know if we have any direct way of defining array of objects something like:



var myArrayObj=new Array(ODT.Classes.New(" TextFieldClass"));



Alternate method could be (I did not try) to create another class and define new arrayObject of first type inside this second class!!!



(Note: Above code may have few errors, it is not compiled one. Only shows the concept)



   

4 Replies

  • Hi,



    There's no way to initialize the entire array at once. You should initialize it item by item either in a loop, or in its contructor like this:

    var myArrayObj= new Array(ODT.Classes.New(" TextFieldClass"), ODT.Classes.New(" TextFieldClass"), ODT.Classes.New(" TextFieldClass"), ODT.Classes.New(" TextFieldClass")/*etc*/);
  • pmahesha's avatar
    pmahesha
    Occasional Contributor
    Continuation of this same thread:



    I have declared new object as:

    var myObject=new Object();

    myObject.property1= something;

    myObject.property2= something;





    Now I want to create an array of myObject type.



    var myObjectArray = new Array();





    How do I create each array elements?

    I tried as below , but seems to be having problem.

    myObjectArray [0] = myObject;

    myObjectArray [1] = myObject;



    How we have to create each array elements? Do we need to say something like:

    myObjectArray [0] = new (myObject)



    Note that the class is not created using ODT. Object is directly created.


  • Hi,



    You first need to define methods of your object as it is described in MSDN.

    After that, initialize each element of your array with a new instance of this object (new instances are created via 'new ObjectName()').