Tuesday, January 29, 2013

DataTypes. Subs & Functions


Links to previous articles in this series:

Data Types
*Note that these calculations can vary a bit so they are not always 100% correct but good enough to cover almost every instance.
Before we go too far into programming we will need to understand a bit about data types. I like to think of data types like boxes we want to put stuff in, when packing a house we need the right size box to hold all of our stuff; a microwave box is too small to fit a couch but is too big for a pair of shoes. The same is true for data. One of the main reasons we choose data types carefully is to maintain a balance between scalability and size. For example if we are storing a Boolean value (1 or 0, true or false) we want to choose a data type that will accomplish the goal without going overboard.  To store our Boolean value we can choose the Boolean data type, (this will always be right as long as we are only using it for Boolean) this type is about 1 byte, so storing 1 million values would take about a megabyte (MB) of memory . Storing the data as a long (more on this type soon) is possible, but overkill, our box is too big for our needs, so storing the same 1 million values would take 7.6 MB. The more memory we have allocated for unnecessary data the less we have for other things and we also take a performance hit.

On the other side of this coin is scalability, I have seen on numerous occasions where someone chose something like 4 bytes of data for their data type assuming that it would never be larger than this, only to find in the future they need more data and need to rewrite several pieces of code. Take for instance zip codes; prior to 1980 all zip codes were 5 digits and most people thought that would always be the case, but now many systems use a Zip+4 which brings the total to 9 digits, or 10 if you include the hyphen. All applications that restricted zip codes to 5 digits had to be re-written.
When programming for small platforms such as cell phones or robotics memory is a premium so special consideration must be given to data types. Larger systems aren’t generally as constricted so it is often better to err on the large side.

This link goes into much more detail about all of the data types used in visual studio if you want to use it as a reference. I will detail the ones I most commonly use after the link, you can use the link to look up the specifics at the link.

·         Boolean:              1 Byte                   Store True/False data
·         Byte:                    1 Byte                  Commonly used for colors
·         Double:                8 Bytes                 Covers most decimal numbers
·         Integer:                4 Bytes                 *Covers smaller whole numbers
·         Long:                   8 Bytes                 *For larger whole numbers
·         String                   Depends               Used for storing text up to 2 billion characters

*In Excel 2003 Integer was as large as you needed for most programs dealing with cell references as the maximum row number was 65,535 and 256 columns for a total of 16,776,960. This easily fit in Integer’s 2.1 billion value range, but Excel 2007+ expanded the rows to  1,048,576 and columns to 16,384 which gave about 17 billion possibilities, greatly exceeding the Integer data type but easily within the Long’s 9 quintillion limit (a quintillion is 1 billion billion), not bad for only 4 extra bytes!

Long story short, use Boolean for Boolean, Double for decimals, String for text, Integer for relatively small numbers, and Long for large numbers. Unless I plan to use a lot of memory, I default to Long for whole numbers.

Using Data Types
Keywords are words that have special meaning to Visual Studio, and they cannot be used as variable names, if you try you will get a “Keyword is not valid as an identifier” error, so don’t worry about doing it by accident. One of the most common keywords you will use is “Dim”, the DIM keyword lets the program know that what comes next is a variable definition.

Using the training file you created earlier, or a new one with a button added named btnDisplayMessage, add the following line of code just beneath the button click even Sub.

Dim boolTest as Boolean

As with controls, it is good practice to preface the name of your variable with the data type, this helps later on in your code with filtering names and ensuring the data type will fit the data you want to add to it.
Then add 2 more lines, the first will assign boolTest a value, the second will display this in a messagebox.

boolTest=0
MsgBox(boolTest)

Notice that boolTest is not in quotes, this is very important; if you want to display the value that your variable currently contains you just use the variable name, if you put it in quotes you would just see “boolTest” in your  messagebox.

Click run, a messagebox should pop up displaying “False”, this is because with Boolean types 0 = False and 1 = True.

Now change your line to say boolTest = 10<20 .="" and="" application="" booltest="" calculates="" depending="" equation="" false="" o:p="" on="" or="" result.="" returns="" the="" to="" true="">
Now let’s add in a new data type and perform a calculation.
Delete the code your wrote, and now create 3 integers, 1 named intValue, 1 named intMultiplier, and 1 named intResult.  Set intValue=5, intMultiplier=10, and then set intResult to the product of the multiplication. Finally, display the result in the textbox (txtOutput if you do not have it).



If you did everything correctly, 50 should appear in the textbox. That covers the basics of creating and assigning variables, performing calculations, and displaying results.

One last note here is related to scope. This code works for the button click, but if you were to try to perform the calculation in the load event of the form you would get errors and your application would not run unless you put the DIM’s in the load event. This is because the DIM’s only work within the event they are created in. If you want to create variables that are accessible from anywhere in your application, you just move them out of your Sub’s and place them just below the first line.



Why not just make the variables accessible to the entire program always? Once you DIM the variables memory is assigned to them and stays with them until the application closes or the Sub using them closes. Say you were creating arrays of variables (more on them later) and you filled your array with 1 million pieces of data. If you create the DIMs at the application level, at the top, the memory will be held until the application closes, but if you put it in the event like our first example the memory is released as soon as the click event completes. Dimming everything at the beginning of a big application can quickly use up all available memory, so it is best to do it only when necessary.

Subroutine and Function
There are 2 primary methods for executing code, the subroutine (Sub) and the function. As mentioned in a previous article a sub performs an action, a function does the same, but is used to return a value. Generally you will see a Public or Private keyword before your subs and functions, I will go into further detail later, but the general idea is that Private means the code can’t be called from outside of the current class.

Taking our code from above, let’s create a sub and a function, starting with the sub.
Start by typing “Sub multiplication” and press enter (note that this must be below the Public Class Form1 and outside of other subs) VS should complete the line as well as add an End Sub below. Next, copy the multiplication data from the button click event above and paste it into this new sub and delete the code in the click event. Your code should look like this:





Now to execute the calculation we have to “call” the sub, we do this by typing the name of the sub (multiplication) before the button click event’s end sub. As you type, intellisense ought to pop up with the name, you can hit tab to accept it, the press enter. This will add parentheses to the end, this is how you can tell it is calling a sub. One handy shortcut that will become more valuable as your code grows larger is hitting Shift+F2 after clicking within the sub name you just typed, this will place your cursor at the beginning of the sub. This is handy when working with a sub later and you need to see what is inside or make a change but don’t want to search all of your code to find it. Hit run and click your form button, you will get the same result as you did when the code was within the click event. The main reason for putting this in a sub vs. in the event itself is when you want to use the same piece of code from several different events.

Now let’s create a function, you can create one from scratch, or just change the word sub to function on the method we just created, notice that the End Sub is automatically changed to End Function. If you run this now you will get the same result as before, but let’s go  a step further to illustrate the power of the function.

You’ll remember that when something is dimmed in a sub (or function) the data is released when the sub ends, functions allow you to return data to other subs or functions. Move your code back up to the button click event above where you called multiplication(), it must be above because code executes from top to bottom. Now delete the line: intResult=intValue*intMultiplier. Within the parentheses on your function type “intVal, intMult”. Your code should look like this:



There are 2 things to notice, first is the green line under the End Function, hovering over it you see that the function doesn’t “return a value on all code paths” which is a problem since the purpose of a function is to return a value. We will fix this soon. Now hover over our second issue, the blue line beneath multiplication(). You will see an error related to an argument not being specified, this is because by placing intVal and intMult in our function that the function now requires 2 parameters. Change the multiplication() line in the click event to multiplication(intValue, intMultiplier) this is called “passing parameters” to your function. Now your function is receiving our variables, which in this case is a 5 and a 10. Next we want to perform the calculation, to do this add the following line to the function: multiplication=intVal*intMult
This performs the calculation and sets the result to the multiplication function. The function will perform right now, but we need to set the result to intResult before we display it, so in the click event type “intResult=” before calling our function, and move the txtOutput line below like this:




Now run your application and click the button, you should get the same result as before. What makes functions so useful is that you can create the function once, then call it from anywhere in your program passing in any variable you want. Just as an example, change your parameters to 10 and 50 and run it again, you will see that the function performs its task on anything you pass in as long as it can be performed.




While you will generally want to create variables, you can simplify all of this by calling the method and displaying it at once:



We will go into more advanced functions in future articles, but this should be enough to get you going for now.





No comments: