Beginner's Guide To Xojo Methods
One user, in the Xojo forum, came up with the request for a very simple tutorial for methods. So a tutorial that explains "methods" without much overhead. I'll give it a try. If you are already familiar with methods, you might want to check this article:
What are methods anyway? The easiest way to think of them is to think of them as functions in mathematics. As a rule, one or more parameters are passed, the method performs a calculation and then as a rule returns this found value to the "caller". With that we are already roughly at the purpose of the methods. We use methods to structure our code and avoid having to insert the same code over and over again.
Let's take as a practical example that we want to create a pdf file. We program a logic that creates the header. Then we fill the rest of the page. At some point we have given out so much text that we need a second pdf page. So far so good, we only need the header again on this second page. We now have the option of copying our code from the first page and pasting it there when creating the second page, or we can pack the code for the header in a separate method and call it whenever we need it. So our code for the header is encapsulated and we can use it for every new page. That also works for a pdf with 1000 pages.
Another advantage is that if we discover an error in our header code, we only have to change it in our method and not in 2, 3 or 1000 places in the code, depending on how large our Pdf has become.
Let's say we have 3 values. The first 2 values should be added and the sum should be multiplied by the third value. Then our code looks like this:
Var value1 As Integer = 10 Var value2 As Integer = 20 Var value3 As Integer = 30 Var result As Integer result = ( value1 + value2 ) * value3
If we need this calculation more often in the program, then it is better if we outsource it to a method.
Let's call the method: mySpecialCalculation. Let's create this method in the IDE:
The screen of your method should now look similar to the below (note that I already moved the original code of our main app into the method - the middle part of the screen). This will not compile, nor work, so please continue to read.
Let's focus first on the right part of the screen:
In Method Name I typed manually the name I want my method to have.
In Parameters I filled in a definition for our 3 parameters. These are the input values, which have to be (mandatory!) used by whomever wants to call this method. In other words something like
will fail. I defined 3 parameters, which mean that the the caller has to call this method now with 3 parameters. So the call has to look for instance like:
Call MySpecialMethod( 100, 34, 56 )
This would work, but won't compile either. Why? I defined a return type, and I said it should be integer. This tells the "caller" that my method will give a value back and that the caller has to use it. That means that the working code for calling MySpecialMethod has to look like this:
Var result As Integer = MySpecialMethod( 100, 34, 56 )
This will actually compile from the interface perspective, but not in case you typed the middle part of my screenshot into your method.
Your main code will now look like this, and return the desired result:
BUT(!) we made a mistake (on purpose). Our main program is now successfully transferring the 3 values to mySpecialCalculation and that method is giving back the result to the variable result in the main app, but the code in our method is wrong! Let's have a look at it:
Actually this code works (no surprise as it is a copy and paste) but it is defining it's own variable in the code and not using the parameters we defined on the right hand side. So let's change the code in our method, to use our parameters. Think of the code in your method as being the code in a complete separate app. You can define your own variables, but of course you should use the parameters you defined for an obvious reason.
So your code should now look like this:
Note than other than defining the parameter to return our result, we are not defining any new variable but we are using our parameters!
To make things (hopefully) even clearer, something like this would work as well:
But it is generally a good advise to define a return parameter, and once you are finished with all your math you write return myReturnVariable. But of course what you want to return has to have the same type than the one you defined in "Return Type" on the right hand side.
The code of our main app looks like follows:
Var value1 As Integer = 10 Var value2 As Integer = 20 Var value3 As Integer = 30 Var result As Integer result = mySpecialCalculation( value1, value2, value3) break
Now let's assume you want to replace the breakpoint by outputting your result to the console with System.DebugLog( result ). Let's try this:
Var value1 As Integer = 10 Var value2 As Integer = 20 Var value3 As Integer = 30 Var result As Integer result = mySpecialCalculation( value1, value2, value3) System.DebugLog( result )
Bad luck, System.DebugLog expects us to use a string, so let's change the code in our main app to the following:
Var value1 As Integer = 10 Var value2 As Integer = 20 Var value3 As Integer = 30 Var result As String result = mySpecialCalculation( value1, value2, value3) System.DebugLog( result )
That looks good, let's give it a try:
Okay, we are making progress, but we now we have an issue with our method. As that one is returning an integer. But that's not a big deal, let's go to our method and change the code (and!) the return type to string.
Here is the code:
// Let's calculate with our parameters Var result As Integer = ( val1 + val2 ) * val3 Var resultStr As String // Let's beautify our output resultStr = "I am proud to present you my result: " resultStr = resultStr + result.ToString + EndOfLine Return resultStr
And the corresponding screenshot of our method:
And the code of our Main App will look like this:
If you run this little app, it will give you the following output in the console:
Sign in or become a blog.xojoDOCs.com member to join the conversation.
Just enter your email below to get a log in link.