Technology Programming

How to Read Visual Basic

    • 1
      Example of a project listing in Visual Basic 6 (Violet Mabe)

      Review some essential Visual Basic concepts. Visual Basic code is usually encapsulated in a project, which is further divided into types such as Forms (representing screens or dialog boxes of a Windows application), Modules (groups of functions that are related), Classes (groups of related functions encapsulated in a specific way) or Controls (representing screens that can be viewed in an ActiveX compatible Web browser, essentially Web versions of Forms). If you are using Microsoft Visual Basic 6, open the project file you want to review, which has a .vbp extension, and you will see a list of folders on the right-hand side of the screen that contain individual code files. The actual code can be found inside the individual files by right-clicking on each file and selecting "View Code."

    • 2). Learn Visual Basic's common data types. You will see these data types over and over again: String, which represents a "string" of text that is always enclosed within parentheses; Boolean, which represents either True or False; Double and its cousin Single, which contain numeric values with decimal places; Integer and its cousin Long, which contain integer numbers (numbers without decimal places); and Byte, which contains an integer number between 0 and 255.
      To declare a variable, Visual Basic uses the keyword Dim in combination with the data type, like these examples:
      Dim iNumber As Integer
      Dim iNumberResult As Single
      Dim ResultString As String

    • 3). Review Visual Basic operators. There are three types of operators: arithmetic, relational and logical. Arithmetic operators are used for math operations, and include: + (plus), - (minus), * (multiply, or times), / (divide), \ (integer division), ^ (exponent), Mod (modulus, or the remainder of division), and & (used to concatenate, or join, strings).
      Relational operators are used to compare data such as numbers and strings, and include: > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), <> (not equal to), and = (equal to).
      Logical operators are used to make decisions and direct the course of the program, and include OR (the statement is true if at least one side of the OR statement is true) and AND (the statement is true if both sides of the AND statement are true).
      Visual Basic also uses the character ' (single quote) to denote comments, or notes, in the code. A single quote at any point means all the text after that quote is ignored when the program is executed.
      Some examples of using these operators:
      iNumber = 7 ' this is a comment: example of equals operator
      iNumberResult = iNumber / 2 ' example of divide operator
      If iNumberResult > 3 Then
      iNumberResult = 3 ' example of greater than operator; we will cover If, Then, Else, and End If in the next step.
      Else
      iNumberResult = 0
      End If
      If iNumberResult < 4 OR iNumber > 7 Then iNumberResult = 0 'example of OR operator

    • 4). Learn Visual Basic conditionals, which allow code to be executed only under certain conditions. The most common conditionals you will see in any Visual Basic or BASIC program are If...Then...Else...End If statements. The If statement allows you to execute a block of code (several lines together) only when a certain condition or set of conditions are met. You can see this in our previous example:
      If iNumberResult > 3 Then
      iNumberResult = 3 ' example of greater than operator; we will cover If, Then, Else, and End If in the next step.
      Else
      iNumberResult = 0
      End If

    • 5
      Example of a Visual Basic 6 project screen (Violet Mabe)

      Practice reading the example above. First look at the code between the "If" and the "Then" statements. This is the statement that is most important, because if this statement is true, the code between the "Then" and the "Else" (or between "Then" and "End If" if no "Else" is present) will be executed. If this statement is false, and there is an "Else" present, the code between "Else" and "End If" will be executed. So in our example here, if iNumberResult is greater than 3, the code will execute to set iNumberResult equal to 3. If iNumberResult is less than 3, the code will execute to set iNumberResult equal to 0.

    • 6). Review loops, which execute the same block of code repeatedly until a condition is met, use a counter to determine how many times to loop, or loop through a collection of objects. Examples of loops include Do While [condition statement]...Loop, Do...Loop Until [condition statement], For...To...Step...Next, and For Each...In...Next. Examples of each:
      ' This Do While loop will loop until the condition statement is false, that is, until iNumber is equal or greater than 100.
      Do While iNumber < 100
      iNumber = iNumber + 1
      Loop
      'This Do Until loop will loop until the condition statement is true, that is, until iNumber is equal or greater than 100.
      Do
      iNumber = iNumber * 2
      Loop Until iNumber >= 100
      'This For Next loop will loop until the counter variable, X, equals the value between "To" and "Step" (100),
      ' incrementing the counter by the value after "Step" (2) each time. This means each time the loop executes,
      ' the value of X will increase by 2 until it reaches 100.
      For X = 0 To 100 Step 2
      iNumber = iNumber + X
      Next X
      'This For Each loop will go through each element in a collection, represented by the variable MyList.
      'Within the loop, the current item from the list is represented by variable MyItem.
      'A collection is like a list of variables. In this case, MyList represents a list of Integers.
      For Each MyItem in MyList
      iNumber = MyItem
      Next

    • 7
      Sample Visual Basic 2008 Express screen after converting a VB6 project (Violet Mabe)

      Review Functions and Subroutines. Within each of the code files you will find a collection of Function declarations, Functions, Subroutines, and Variables.
      Functions, which begin with a statement similar to "Function SomeFunctionName(SomeVariable As String, SomeOtherVariable As String) As String" and end with the statement "End Function," are designed to take inputs, which are the variables in the parentheses after the function name ("SomeFunctionName" in our example), and to return an output, which is of the type specified following the end parenthesis ("As String" in our example). This example function takes two parameters, the strings SomeVariable and SomeOtherVariable, and returns a string. When this function is used, you would see a line like the following:
      ResultString = SomeFunctionName("OneInput", "TwoInput")
      If this function was within a Class file, and was called outside of that class, the line would look something like this:
      ResultString = MyClassName.SomeFunctionName("OneInput", "TwoInput")

    • 8). Learn how classes and libraries are accessed in the code. Visual Basic uses a standard "dot" structure for referencing functions and objects in classes and external libraries. Suppose you are looking at code that uses an external library to get information from Google Maps, which is called GoogleMaps. You might see lines similar to the following:
      Dim GoogleMapObj As GoogleMaps
      Dim GoogleLocationObj As GoogleMapsLocation
      GoogleMapObj = CreateObject("GoogleMaps") ' This line creates the GoogleMaps object
      GoogleLocationObj = GoogleMapObj.GetLocation("Seattle, WA")
      You can see how the dot or period is used to reference a function within the GoogleMaps library.

    • 9). Put it all together by going through the code you are reviewing line by line. Remember the concepts you have learned, and consult the links at the end of this article for additional resources that may help you with advanced code or code that is difficult to understand.

Related posts "Technology : Programming"

Brochure Design And Its Budget

Programming

Website designing delhi-web design services India-Website Development Company India

Programming

The Secrets and techniques Rob Fore Won't Tell you!

Programming

Where Do You Get Podcasting Ideas?

Programming

Avoid Hacking With The Help of WordPress Development Company

Programming

Sirius Radio - All You At Any Time Wished to Know

Programming

How to Use a Check Box to Filter a List

Programming

Get professional help from website development Dublin- promote business growth

Programming

PHP Shopping Carts

Programming

Leave a Comment