All in the same code flow?

Hello my dear gurus,
here is something is any way to do something like this “on the same code line”?
i mean, without creating a function or method? i main, in the same block where i’m coding?
is it with “SUB”? if so, how?

example of what i would like to do:

var a as string
a = “NO”

function change
a = “Hola”
end

msgbox a //it will show “NO”
call change()
msgbox a //will show “Hola”

Thanks!!!

instead of call change just do: a = "Hola"

As Bjorn said, do you mean this?

var a as String = "NO"

MessageBox a 'Shows NO
a = "Hola"
MessageBox a 'Shows Hola

Or do you mean can you have a function that is actually written inside another sub/function? That isn’t possible in Xojo.

oh no… sorry if i wasn’t clear.

the a = “Hola” was just a bad example, inside that function there will be a lot of lines, that i need to call several times.
I know i can to this just creating a method but i wonder if i can just write a “block” inside a code line and call it several times.
I know it is a “bad practice” but remember basic? when at line 100 you can go/call/jump back to line 60 and then (after you did some code there) it comes back and continue at line 101?

let’s say: i would like to create a method inside a method, and be able to call it from inside that method, instead to just see like: call myfunction(), and had to navigate to xojo method navigation bar, find myfunction, and then… see what is happening in there. and after, i have to navigate again back to my code.

make sense? I saw things like “SUB [] … SUB end” but i can’t figure it out to make it work in my code.
thank you in advance and i’m sorry if i’m still not clear…

Javier

You might look at XojoScript.

In Xojo, you could do your own flow control of code in the same routine using Goto – but few would probably say that’s a good idea.

No, you’d create a method.

You could also use a Delegate, if what you are trying to do is pass a function to execute into another function (similar to a lambda in other languages). You will still need to create the function to execute as a separate function in the project though.

you could use a loop with select case inside
and exit at your condition.

but a longer running method will block your main ui thread.

I think he’s trying to use the ide like a text editor.

3 Likes

I think you are talking about GoTo

var a as string
a = "NO"

GoTo myMessage

myChange:
a = "Hola"

myMessage:
MessageBox(a)

if a = "Hola" then GoTo myEnd
GoTo myChange

myEnd:

Is better to avoid this.

1 Like

Doing what the OP seems to want using anything other than a method is foolish.

I remember a long time ago a colleague at CERN writing something like the following (this was in FORTRAN):

10 // first line of code in this block
// Lines of code here
// last line of code in this block
goto (20, 30), i

i = 1
goto 10            // (actually first method call)
20 continue        // continue does nothing in FORTRAN

// more lines of code

i = 2
goto 10            // (actually second method call)
30 continue

// more lines of code

This was a small part of a long FORTRAN subroutine, but after 30 mins or so I had worked out that the code at first/last lines in this block were themselves a subroutine, with a computed goto at its end in order to effect the Return. He called the method twice.

Once I had that figured out, I was able to re-write the whole thing to be more reliable, more obvious, and much easier to maintain.

Lesson: use the features the language provides.

3 Likes

sure you can write a longer method and later it make sense to split parts of it into single methods or encapsulate into a class.
a rule in past, do not write a single method with more than 64kByte source code.
small methods (and without usage of globals!) are easier for testing & debugging and good to reuse.

1 Like

It’d be much better, and easier, to have a long descriptive name for your function (ChangeVariableToHola(s as String), CalculateAreaGivenPoints(), or whatever) to know what it’s doing rather than to try to avoid dealing with the IDE navigation. Yes, it can be a pain jumping back and forth in the IDE because of some of it’s glitches, but you will likely hate yourself in the future if you try to do something complicated with goto. And anyone else who sees your code in the future will definitely hate you. LOL

Is your goal to change a as fast as possible or to save yourself time navigating around your code?

If you pass a byref it will be almost as quick as not having a method and you will be able to change a in your method directly.

If you want to save time, do you know about right click goto method to save time finding the method in the navigator? You can the use the big back arrow at the top to jump back.

1 Like