calling method with dynamically generated method name

I’d like to call a method with a dynamically generated method name. So for example if I have a method, named “AtoB”, and have Var1 = “A” and Var2 = “B”, I’d like to be able vary the method name with something like: callMethod(Var1+“to”+Var2). Is that possible?

Yes, using Introspection.

https://documentation.xojo.com/api/language/introspection.html

In brief, you use Introspection to get all the methods of a class, then loop through them until you match the name and signature you’re looking for. Once you’ve found it, use Invoke to call it.

That’s awesome, than you so much, Kem! Will have to dig into this though. Doesn’t look very easy.

my question would be what is it you’re doing that you need to calculate the name of the method to call ?
and what are you going to do if you compute a method name that doesnt exist ?

I have a suspicion that there may be other ways to solve this problem
introspection is often the sledgehammer for cracking peanuts

its something like call by name.
if there is only a few you could also use a select case.
select case name
case “AToB”
AToB a,b,c
end select

there are other options like deciding not to actually decide and using polymorphism to “do the right thing”

but without knowing what he’s trying to accomplish and why he thinks he needs to do this its hard to give advice

In PHP I had some methods to process text that could come from a file or from a socket. So I had separate methods to read from each source, as there was some extra source-specific code in each of the two methods.

Then I’d pass a parameter to any of the processing methods which would be reading the text in to process it. So on the calling side it’d be like:

$myreader = "readfromDisk"; // or $myreader = "readfromSocket"; $result = textMethod ($myreader, ...)

and inside the processing method:

$nextline = $myreader ($arg1, ...);

That struck me as cleaner than passing a boolean into textMethod() and then doing like:

if ($readingDisk) { $nextline = readfromDisk ($ag1, ...); } else { $nextline = readfromSocket ($arg1, ...); }

I eventually found out how to do something similar in Xojo.

In Xojo, you take advantage of the fact that both the socket and the TextInputStream/BinaryStream implement the Readable interface. Make the first parameter of your method be of type Readable:

Function textMethod(myreader as Readable, ...)

Then you can pass an inputstream or a socket and the method doesn’t need to know which it is.

[quote=478614:@Tim Streater]In PHP I had some methods to process text that could come from a file or from a socket. So I had separate methods to read from each source, as there was some extra source-specific code in each of the two methods.

Then I’d pass a parameter to any of the processing methods which would be reading the text in to process it. So on the calling side it’d be like:

$myreader = "readfromDisk"; // or $myreader = "readfromSocket"; $result = textMethod ($myreader, ...)

and inside the processing method:

$nextline = $myreader ($arg1, ...);

That struck me as cleaner than passing a boolean into textMethod() and then doing like:

if ($readingDisk) { $nextline = readfromDisk ($ag1, ...); } else { $nextline = readfromSocket ($arg1, ...); }

I eventually found out how to do something similar in Xojo.[/quote]

yeah I would not sonider doing something like that in Xojo - certainly not that way
Personally it would probably involve an interface like readable and two classes that implement that interface - one that reads from disk and one that reads from a socket

And, by doing this, I’d even get rid of the if statement