The Kinds of Lines in Visual Basic

A Visual Basic program is composed of multiple lines of source code that is compiled into an executable. Source code lines can include declarations that set aside memory for a specific type of data that will be accessed by the defined name and executable statements that perform calculations. They can also include input or output, and control of flow statements that cause execution to occur down a specific path. A Visual Basic program should also include documentation about how the program works.

  1. Lines of Code

    • A line of code is one that is compiled into binary for execution. The compiler knows that the end of the line has been reached when it encounters a carriage return or a line feed character.

      The following line of code is a variable declaration that sets aside memory to store an Integer value that can be accessed by the name intAge:

      Private intAge As Integer

      The following line of code adds 1 to intAge as stores it in a variable named intNextAge:

      intNextAge = intAge + 1

    Explicit Line Continuation

    • Some lines of code are very long. To make long lines of code easier to read, Visual Basic allows you to add a space and an underscore ( _) before the carriage return to cause the compiler to ignore the carriage return. Here is an example of a line of code that uses explicit line continuation.

      strDescription = "This product was manufactured by " & strMfg _

      & " and is available in " & strStyle1 & ", " & strStyle2 & ", " & strStyle3 & ". " _

      & strProductSummary

      The & operator is the Visual Basic string concatenation operator. The code concatenates literal text and variable values and stores the resulting text in a variable named strDescription.

    Implicit Line Continuation

    • One of the enhancements introduced in Visual Basic 2010 is the ability of the compiler to implicitly recognize places where the line should be continued, even if there is no line continuation character. For example, the compiler knows that a line of code should not end in a comma, a mathematical operator, or an open parenthesis. So if it does, it ignores the carriage return.

      For example, the compiler would treat these two blocks of code the same way:

      Dim decPayCheck As Decimal

      decPayCheck = decGrossPay - decFedTax - _

      decStateTax - decFICA

      Dim decPayCheck As Decimal

      decPayCheck = decGrossPay - decFedTax -

      decStateTax - decFICA

    Preprocessor Directives

    • A preprocessor directive is a special type of line that is processed before the program is compiled. It is used to designation areas of the program that should only be compiled under certain conditions. A preprocessor directive begins with the # character.

      For example, the following code compiles a different version of the Sub Procedure SetPrice if forRetail is True than if forRetail is False.

      #Const forRetail = True

      #If forRetail Then

      Public Sub SetPrice

      End Sub

      #Else

      Public Sub SetPrice

      End Sub

      #End If

    Comments

    • A comment is a special type of line that is ignored by the compiler. You create a comment by beginning a line with either a single quote (') or the keyword REM. The compiler ignores the entire line, including a line continuation character. Therefore, when creating multiple line comments, you need to begin each line with either ' or REM. Here is an example of a comment:

      'Subtract federal, state, and FICA taxes from gross pay

      Comments are useful for documenting programming logic to make the program easier for a programmer to understand.

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured