XOJO method return statement

This is a simple beginners question. Below is the complete project including descriptions of the controls.

Controls

  1. Button1
  2. Textfield1
  3. Textfield2

Button1 Pressed

var answer as string
var a as string
if textfield1.text <> “yes” and textfield1.text <> “no” then
close

else
answer = textfield1.text
a = methodzero(answer)

end if

textfield2.text = a

Window1 Methods

Windowzero(as string) as string
// takes a string parameter and returns a string parameter
t = method1
else
t = method2
end if

return t

Method1

//returns string

var t as string
var s as string
s = “you said yes”
t = s
return t

Method2
//returns string

var t as string
t = “you said no”
return t

The program runs with output close, “you said yes” or “you said no”
Method1 returns a string.
Method2 returns a string

but
Methodzero either closes or returns method1 or method2 through ‘T’ as string variiable.

How is this possible?

When posting code in a post, please do this:

  1. Add the code
  2. Select it with the mouse
  3. Click </>

Otherwise I tend not to read the post.

3 Likes

It really isn’t.

How is this possible?

How is it possible that the method closes or returns?
Or how is it possible to write such a dialog?

Perhaps you are planning ahead, but you don’t need Method1 or Method2 methods to do this.

Maybe all you need is:

Button1 Pressed

if textfield1.text <> “yes” and textfield1.text <> “no” then
close

else
if  textfield1.text = "Yes" then
textfield2.text = "You said yes"
else
textfield2.text = "You said no"
end if

Windowzero is the method name? or Methodzero?
Do you have an if for this?

really tried to be careful with my copying and pasting but apparently a gremlin ate some of my code. Here
is the code for Methodzero:

var t as string
if a = "yes" then
  t = method1
else
  t = method2
end if

return t

As you can see Methodzero returns either Meihod1 or Method2 with the
string variable T. I’m trying to understand why that is allowed.

Yes, I’m planning ahead. This came out of another project where it was actually necessary to do this. I just simplified things so that all would be left is the question as to how a method could return a string variable as a method.

Can you create the sample project and share it using Dropbox, One Drive, Google Drive?
That will be easier/faster to review.

1 Like

That’s not what you’re doing. You are retrieving a string from one or the other method. Then you are returning that string. You could manipulate that string in any number of ways before you return it. Once you obtain the string from the method, it no longer has any connection to the method. It’s just a simple string.

1 Like

see it as

t = method1()

it call this method without argument, the method return a string.

This is why I always supply the () for any method I call, even with no parameters. I’ve not come across another langauage that doesn’t insist on the params, and supplying them makes it quite clear that I am making a method call.

2 Likes