March 29, 2024, 04:10:33 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Return a Class

Started by kirkkaf13, August 18, 2020, 08:30:50 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kirkkaf13

Is it possible to return a class from a subroutine? If so could I have a small example as it's not as obvious as I thought.

Thanks.

aurelCB

That is a strange question

In general OOP class is a collection of methods or functions or subroutines and as such you can return one of 
members of class not class from member....i never tried .

kirkkaf13

I don't think it's a strange question. A class is just a type just like an integer or string. I have used Java for many years and can return an instance of a class as a return type.

Maybe the use of class has mislead you. I mean an instance of a class, an object.


aurelCB

August 19, 2020, 04:31:56 AM #3 Last Edit: August 19, 2020, 04:51:02 AM by aurelCB
Look
I am not sure how exactly OOP is created in IWB
and in general i don't like OOP as such

but from your question it looks to me that you want return UDT member-object from
subroutine...


from user guide:

CLASS employee
    'methods
    DECLARE employee( ) : 'The object constructor
    DECLARE _employee( ): 'The object destructor
    DECLARE CalculatePay(hours as float),float
    DECLARE SetPayRate(amount as float)
    DECLARE SetEmployeeID(id as int)
    DECLARE PrintPaycheck( )
    'members
    int m_employeeID
    float m_payrate
    float m_lastpay
    string m_name
    pointer m_history
END CLASS

kirkkaf13

Looks like it's working now. Here you can see createPerson() returns a instance of the person class.

$main

class person
private
string name
public
declare getName(), string
declare setName(string n)
end class

sub person::getName(), string
return name
end sub

sub person::setName(string n)
name = n
end sub

sub createPerson(string name), person
person p1
p1.setName(name)
return p1
end sub

person p1 = createPerson("Aurel")

print p1.getName()

aurelCB

I know that must work ..but as i said ,...i don't use OOP even try to avoid UDT when i can.
good luck ..and read Userguide  ;)