3 programming tips in Visual Basic

 May 29, 2014

As per the Microsoft Developer Network website, Visual Basic is engineered for productively building type-safe and object-oriented applications. Here are 3 tips I’ve found to be most useful when working with Visual Basic. Try it out for yourself. Tip #1: String Comparisons Quite often, you may need to compare two string values together to see if they are equal. In almost all programming languages, including strands of Visual Basic (VB), the string comparison is case sensitive. For example, let’s say you are reading a file or getting input from the user, and putting them in a variable called ‘myWord.’ Now, you want to see if myWord is equal to “How are you”. If so, then you will send a reply to say “I am well thanks.” Normally in VB you would write something like this:
If myWord=”How are you” then msgbox “I am well thanks”
This may well work for you only if the user types “How are you” exactly the way you expect, that is, everything in lowercase except the ‘H.’ What if the user types “how are you”? Although the user is greeting, the program cannot find it. To make comparisons case insensitive, you need to raise the variable to either lowercase or uppercase in both sides of the comparison operator like this:
If LCase( myWord )=”how are you” then msgbox “I am well thanks”
Or like this:
If UCase( myWord )=”HOW ARE YOU” then msgbox “I am well thanks”
As you can see, in the first instance, the user input is turned to all lowercase (using LCase function) and compared with all lowercase “how are you”. In the second instance, the user input is raised to all uppercase and compared with “HOW ARE YOU”. So no matter what case the user sends you the greetings, you can always pick it up and reply politely. Tip #2: Switch a Boolean On or Off There are many occasions where you want to set something ‘On’ if it is ‘Off’ and set it ‘Off’ if it is ‘On.’ For example, you need to set an Excel cell to be bold if it is not already bolg, but turn the bold off if it is already bold. A lot of programmers write it like this:
If Range(“A1”).Font.Bold = True then Range(“A1”).Font.Bold =False Else Range(“A1”).Font.Bold = True
A simpler way to write it looks like this:
Range(“A1”).Font.Bold =NOT Range(“A1”).Font.Bold
Since Range(“A1”).Font.Bold is a Boolean (either True of False), then no matter what state it is at any point in time, when the above line is executed, its state will change because of the keyword NOT. Tip #3: Three more tips for writing your code Every time you create a programming block such as If, Do, For, With, etc., try closing it straight away before writing the body in them. For example, as you finish writing the line “If … Then”, press Enter twice, type “End If,” and then go up one line to type the body inside your IF block. This way, you won’t need to remember to close the block because you already have, and so you minimise compile errors. If you want to copy a line in VB, put your cursor anywhere in the line and press Ctrl+Y followed by Ctrl+V. After this, go to where you want to paste the line and press Ctrl+C. You can comment out a block of code by following the steps below:
  1. First activate the ‘Edit Toolbar’ by right clicking on the ‘Main Toolbar’ and selecting ‘Edit.’
  2. Grab the floating Edit Toolbar by its title bar and move it on the ‘Main Toolbar’ to dock it.
  3. From now on you can comment a block of code by highlighting the block, then clicking on the ‘Comment Block’ icon on the Edit Toolbar.
  4. You can also un-comment a block of code by clicking on ‘UnComment Block’ icon.

How do your Excel skills stack up?   

Test Now  

About the Author:

Cyrus Mohseni  

Having worked within the education and training industry for over 15 years as well as the IT industry for 10 years, Cyrus brings to New Horizons a wealth of experience and skill. Throughout his career he has been involved in the development and facilitation of numerous training programs aimed at providing individuals with the skills they require to achieve formal qualification status. Cyrus is a uniquely capable trainer who possesses the ability to connect with students at all levels of skill and experience.

Read full bio
top