Class/Object Array

Thanks so much everyone, This has really helped. The Class interface kinda worked. But could you please link me to somewhere I can learn superclasses and subclasses I can’t seem to find anywhere.

I’d go through the documentation set that is already installed on your computer with Xojo (Right next to it in the documentation folder and accessible from the Help menu)
Or you can download things from http://documentation.xojo.com
There are 5 books there (4 users guide & 1 learning programming)
As well there are tons of videos and webinars

After much fighting with my code to find the right solution to declaring an array of a class i defined i found the following solution. I share it with you here because i have a feeling this is not terribly obvious.

[code]Dim PlusList() As OperandPair // PlusList() is a property and OperandPair is my class definition with no superclass
ReDim PlusList(NPlus - 1)

For i as Integer = 0 to 9
for j as Integer = 0 to 9
Dim pos As Integer
pos = i * 10 + j
PlusList(pos) = new OperandPair // This was not obvious to me because without this line the debugger shows that
// PlusList is in fact of type OperandPair(99) which is correct.
PlusList(pos).Lower = i // Without the preceding line i get a NilObjectException when referencing PlusList(0)
PlusList(pos).Upper = j
Next
Next[/code]

So, even if you declare something as an array of class, which passes the compiler and runtime. and the compiler says that it is an array of that type of the proper length, you still have to allocate each object in the array. This is not obvious in the documentation. Is there a better approach than what i’m doing?

You’ve create a spot for a new object, but haven’t actually created it.

Let’s forget about arrays for a second and use a single variable with Date, just for the purpose of illustration. Look at this code:

dim d as Date
dim d as double = d.TotalSeconds

If you try to run this code, you get a NilObjectException because you haven’t actually created a new Date in d. Instead you need to do dim d as new Date or, on the second line, d = new Date.

It’s the same with an array. If you say dim arr( 99 ) as Date, you’ve created 100 spots for Dates, but haven’t actually created any.

In you case, revise your code like this and it should work:

Dim PlusList() As OperandPair // OperandPair is a class i defined that has no super class and PlusList() is a "property"
ReDim PlusList(NPlus - 1)

For i as Integer = 0 to 9
  for j as Integer = 0 to 9
    PlistList(i * 10 + j) = new OperandPair
    PlusList(i * 10 + j).Lower = i // Fails here at run time on the first access of PlusList(0)
    PlusList(i * 10 + j).Upper = j
  Next
Next
ReDim PlusList(NPlus - 1)

Resizes the array PlusList to be able to contain “NPlus -1” objects. This statement does not create the objects if they are not standard types like integers, strings…

You have to create instances of your class and add them then to the array, before you can access them there.

For example:

dim arrOfMyclass() as MyClass
dim i as integer
dim mc as MyClass

for i = 0 to 9
  // create an object (instance of the class)
  mc = new MyClass()

  // add the object to the array
  arrOfMyclass.append(mc)
next

// Now you can access the objects in the array
// your class has to have a "Lower" property
arrOfMyclass(0).Lower = 17

I wrote the eample out of head. It should be enough to show how it works.
If “arrOfMyclass(0).Lower = 17” would create a NilObjectException then the reason is, that there is no object stored in arrOfMyclass(0)

Edit:
Kem you were a second faster :wink:

Torbin and Kern,

Thanks for taking the time to look at the problem and offer feedback.

Now its all “obvious”, but i think for many people since there are no good examples of this in the documentation, it would help if they added a simple example in the arrays section.

… jeff

This belongs in the Users Guide or Intro to programming book

Probably not the language reference as the language reference isn’t a “how to guide” but “here’s the syntax for how this aspect of the language and framework works” Quite literally short hand

The issues noted seem like a lack of clarity about the difference between arrays of intrinsic types (integers, strings etc) and arrays of references to objects

In both cases the “slots” in the array ARE initialized
When its an integer the value is set to 0, for a string its “”
For an array of object references (note its NOT an array of objects) the references are initialized to NIL (the default for a reference)
So it’s all consistent - but if you aren’t clear on the difference between an intrinsic type like a string or integer & an object reference then I can see how you’d be surprised

I’ve referred this to Paul for his consideration to see what he wants to do with it