IonicWind Software

IWBasic => Object Oriented Programming => Topic started by: Guilect on October 20, 2008, 04:50:43 PM

Title: defining an Array of a Class question
Post by: Guilect on October 20, 2008, 04:50:43 PM
What is the correct way to define an array of a Class?

I tried the following (compile as console) :


Class foobar
def m_x as int
declare foobar()
declare setx(value as int)
declare getx(), int
ENDCLASS

sub foobar::foobar()
m_x = 99
ENDSUB

sub foobar::setx(value as int)
m_x = value
ENDSUB

sub foobar::getx(),INT
return m_x
ENDSUB



' Test code for above Class
def test[3] as foobar

print test[0].getx()
print test[1].getx()
print test[2].getx()

DO:UNTIL INKEY$ <> ""

But the output is :

99
0
0

and I have checked to see that the Class constructor sub routine is only being called one time.

Thanks in advance for any help.
Title: Re: defining an Array of a Class question
Post by: Ionic Wind Support Team on October 20, 2008, 05:29:08 PM
It is a limitation of the language.  The constructor is only called for the first element of an array.

You can call it yourself for the other elements.

for x = 1 to n
test[ x ].foobar()
next x

Title: Re: defining an Array of a Class question
Post by: Guilect on October 20, 2008, 05:44:20 PM
OK.

Thanks.