IonicWind Software

IWBasic => Object Oriented Programming => Topic started by: Guilect on September 05, 2008, 06:12:37 PM

Title: passing one class to another - how to do it?
Post by: Guilect on September 05, 2008, 06:12:37 PM
First, can it be done?
If so,how?

I tried without luck the following:
Class foo
def x as INT
def y as INT

declare somethingorother()
ENDCLASS


Class bar
declare myfunc(thing1:ANYTYPE), INT
ENDCLASS

'sub bar::myfunc(thing2:foo), int
' Return thing2.x
'ENDSUB

'sub bar::myfunc(thing2:ANYTYPE), int
' def object1 as foo
' object1 = *<foo>thing2
' Return object1.x
'ENDSUB

'sub bar::myfunc(thing2:pointer), INT
' thing2 = NEW(foo,1)
' Return #<foo>thing2.x
'ENDSUB
Title: Re: passing one class to another - how to do it?
Post by: LarryMc on September 05, 2008, 07:20:52 PM
show me all the code (running program) and I'll see if I can help you.

What you posted won't do anything as it it is posted. It's incomplete.

Larry
Title: Re: passing one class to another - how to do it?
Post by: Ionic Wind Support Team on September 05, 2008, 07:38:46 PM
sub bar::myfunc(foo thing2), int
   Return thing2.x
ENDSUB

Would works as lang as the class definition for foo comes before the method, such as from an include file which is where class definitions belong.  Otherwise use a pointer.

sub bar::myfunc(pointer thing2), int
   Return *<foo>thing2.x
ENDSUB

Don't use the ANYTYPE type unless you really know what you are doing, it is a special 64 bit type and not a pointer.

Paul.
Title: Re: passing one class to another - how to do it?
Post by: Guilect on September 05, 2008, 08:35:34 PM
@ Larry,
thanks for the offer of help.
I should have mentioned that the 'incomplete' code did not compile.
It was not intended to do much.
I was just looking for the proper syntax.

@Paul,
thanks for your help.
But I am in need of a little more guidance.
I think I implemented the code as you suggested but, it produces errors :


(a complete app. to test the concept)(In a real app. I would put the Class definitions in a seperate included file).


Class foo
def x as INT
def y as INT

declare somethingorother()
ENDCLASS


Class bar
declare myfunc(pointer thing1), INT
ENDCLASS


'sub bar::myfunc(foo thing2), int
'   Return thing2.x
'ENDSUB

sub bar::myfunc(pointer thing2), int
   Return *<foo>thing2.x
ENDSUB


def f as foo
f.x = 99

def b as bar

MESSAGEBOX(0, STR$(b.myfunc(f)), "woot",0)
Title: Re: passing one class to another - how to do it?
Post by: Ionic Wind Support Team on September 05, 2008, 08:46:47 PM
Your parameters have to match in the definition and implementation.  In your definition you have the parameter named thing1, but in the implementation you are calling it thing2.  In Emergence BASIC the parameter name in the implementation is a placeholder and not used.

'the name 'thing1' is the actual parameter name.
Class bar
   declare myfunc(pointer thing1), INT
ENDCLASS

'the name 'thing1' here is actually a placeholder.  Even if you called it "blah1" you would still need to use the name in the declare statement when referencing the variable.
sub bar::myfunc(pointer thing1), int
   Return *<foo>thing1.x
ENDSUB

Paul.
Title: Re: passing one class to another - how to do it?
Post by: Guilect on September 05, 2008, 09:12:00 PM
Paul,

Thanks.

I had been using a another language were the prototype could have any variable in it as long as the type and number of parameters was correct.