Showing results for 
Search instead for 
Did you mean: 
Create Post

Visual Basic 101 (part 3)

Level 15

In part one this series, I described installing the Visual Basic IDE. In part two, we created the foundation of the bandwidth calculator with all the necessary objects put into place. Now, in part three, we'll discuss the coding.

calc (2).pngide2.png

Lesson 4 - The Coding

The first question you may have is, "Where does the code go?" From the design screen, you can either double-click the form (or any object on the form) to be taken to the Code View, or, click the highlighted red icon above the Properties window in the Solution Explorer, as shown below on the right. All of the code for this project will fall between Public Class frmMain and End Class, also highlighted below:


What Does the Code Mean?

The Visual Basic code we will write is a set of specific instructions designed to do what we want within our program, in this case, calculate figures based on certain parameters. Below is a code snippet taken from the calculator with a detailed explanation of what each element is and does. Be aware that this is only a small bit of code. Explaining every line of the calculator in detail is simply not practical. The point of this exercise is to provide an introductory explanation, hoping you'll pursue more on your own. In the end, you will have a working bandwidth calculator for SAM's components.

Code Snippet Explained

The apostrophe (') is a keyword used in Visual Basic to denote a remark or comment. The original keyword in BASIC was REM (which still works) which is short for "Remark." Anything following either of these two keywords will not be executed when the program is run. I've added comments to each line. Comments appear as green text. VB keywords appear as blue. Note: Code is normally indented to make it easier to read.

Note: The only way to get this calculator is to build it following the steps in this series. It will not be available for download nor will SolarWinds offer support for this tool. The code for this tool is made available for educational purposes only.

Private Sub WMICalc() ' This is the beginning of a subroutine (aka: sub, a block of code named WMICalc). This subroutine ends with END SUB, at the bottom. PRIVATE means (in essence) that this code will not execute unless called upon.

On Error Resume Next ' ON ERROR is a keyword that tells the code to do something in the event of an error, in this case, RESUME NEXT (do nothing differently).

    WMIStat = 315 ' WMIStat is a variable we created that holds a number, which can change. In this case it is equal to 315.

    WMINeededBW = WMIStat * WMIMonitorNumbers 'This is a formula using three numeric variables to perform multiplication, as denoted by the asterisk (*).

    WMIConvert = ((WMINeededBW / 1024) * 😎 / 1024 ' Another formula of division and multiplication with variables and numbers. Any formula within parentheses will execute first.

    'If...Then statement. This is a conditional meaning, if this happens, do this, if not, do that.

    If WMIConvert = 0 Then lblWMI.Text = "00.00 Kbps" : DoTotal() : Exit Sub

    ' In the line above, if a variable (WMIConvert) is equal to zero, then the code will set the label's text property to read, 00.00 Kpbs. If not, the next line of code will be read.

    ' The colon (:) in the above line acts like a new line of code. If the IF statement is true, first - change the text of lblWMI, then navigate to the sub called DoTotal, then Exit the Sub.

    'The below If...Then statement incorporates the Else statement. So we have IF, THEN, ELSE.

       If WMIConvert < 1 Then ' If the value of the variable, WMIConvert, is less than 1, execute the code below until the ELSE statement is reached. If not, execute the code below the ELSE statement, ending on the End IF statement.

        DoTotal() ' Call the sub, DoTotal and execute the code of that sub.

        WMIsuffix = " Kbps" ' This string variable is now equal to the text within the quotes.

        WMIConvert2 = WMIConvert * 1024 ' The variable, WMIConvert2 is equal to the value of the variable, WMIConvert, multiplied by the number 1024.

        lblWMI.Text = Format(WMIConvert2, "###,###") & WMIsuffix ' The text property of the label, lblWMI, is equal to the number held in the variable WMIConvert2 AND (&) the variable WMIsuffix.

        ' The Format(WMIConvert2, "###,###") formats the value of the variable WMIConvert2 to be in proper numerical readable format (999,123, as opposed to, 999123). In this case the comma is added after every three numbers. The ampersand (&) does not do addition, rather, it concatenates, or joins the two variables (e.g. 5 & 4 would result in 54)

        Exit Sub ' Work is done in this sub, perform no more actions


        DoTotal() ' Call the sub named, DoTotal, and execute the code of that sub.

        WMIsuffix = " Mbps" ' This string variable, WMISuffix, now stores the text within the quotes.

        lblWMI.Text = Format(WMIConvert, "standard") & WMIsuffix ' The text property of the variable, lblWMI, is equal to the number held in the variable WMIConvert AND (&) the variable WMIsuffix. The value is formatted using 'Standard'.

        Exit Sub ' Work is done in this sub, perform no more actions

    End If ' The required statement to close a multi-line If...Then...(Else) statement.

End Sub ' This is the end of this subroutine.

What's Next?

Next is the final installment, the code. In part four, I will reveal all the code for this calculator as well as how to compile it so you can use it and share it with your friends. You need simply copy and paste the code and follow a few simple instructions.


None. You've been good thus far and I don't like homework either.

About the Author
Who am I? • I met Robert Frost at the end of the road less traveled, and then pointed him in the right direction. • Einstein asked me to define "Up," and I did. • I cliff dive from airplanes. • On Christmas, Santa comes to me for gifts. • I play three-cushion billiards with one hand. • Lions ask for my protection (I speak Lion). • Bobby Fischer and I came to a stalemate while playing chess. • I have literally given a woman the shirt off of my back. • I have also helped an old lady cross the street. • I know what a dangling participle is. • Mozart bequeathed his Requiem to me, and I corrected it. • I was thrown out of an Eric Clapton concert twice in the same night for drawing too much attention to myself. • I am a verbose minimalist. • I am Bronx. Who are you?