March 28, 2024, 11:23:49 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


passing one class to another - how to do it?

Started by Guilect, September 05, 2008, 06:12:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Guilect

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

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

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.
Ionic Wind Support Team

Guilect

@ 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 :


  • undefined variable - thing2
  • unsupported use of *

(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)

Ionic Wind Support Team

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.
Ionic Wind Support Team

Guilect

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.