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”
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…
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.
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.
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.
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.