Answered How to method chain in class?

markluizdemauro27

New Member
For example:

Define variable classCar as class Car.
define variable carName as character.

carName = classCar:setName("mark"):getName().
 

markluizdemauro27

New Member
I just have to return the instance of the class in setName method so that I can also call the getName method.

method public Car setName(name as character).
mName = name.
return THIS-OBJECT.
end method.
 

RealHeavyDude

Well-Known Member
IMHO your requirement does not make any sense to me.

On your variable classCar you already have the pointer to the object instance - why would you invoke a method just to get the pointer to the object instance? When you new a class you are actually instantiating the object and already have the pointer to the instance on the corresponding variable.
 

markluizdemauro27

New Member
IMHO your requirement does not make any sense to me.

On your variable classCar you already have the pointer to the object instance - why would you invoke a method just to get the pointer to the object instance? When you new a class you are actually instantiating the object and already have the pointer to the instance on the corresponding variable.

First question, do you know what method chaining is? or do you have better ways to do that?
 
Last edited:

markluizdemauro27

New Member
Yes I do. Nevertheless - your artificial example with artificial code does not make any sense to me.

Well what I did is to return the object of the class's instance so that I can use it again for calling another method by a 'single blow'. You were pointing out before that I can do this instead:

classCar:setName("Mark").
carName = classCar:getName().

but because the return type of the method 'setName' is class and you have returned the class instance, then you can use it to call another method.
 
Top