Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 3
The Visual Basic Language

If you need an immediate solution to:
Declaring Constants
Declaring Variables
Selecting Variable Types
Converting Between Data Types
Setting Variable Scope
Verifying Data Types
Declaring Arrays And Dynamic Arrays
Declaring Subroutines
Declaring Functions
Preserving Variables’ Values Between Calls To Their Procedures
Handling Strings
Converting Strings To Numbers And Back Again
Handling Operators And Operator Precedence
Using If…Else Statements
Using Select Case
Making Selections With Switch() And Choose()
Looping
Using Collections
Sending Keystrokes To Other Programs
Handling Higher Math
Handling Dates And Times
Handling Financial Data
Ending A Program At Any Time

In Depth

This chapter is all about what makes the various parts of a Visual Basic program work: the Visual Basic language itself. In this chapter, we’ll see the components of the Visual Basic language and how to use them. After designing and creating the interface for your application using the Visual Basic IDE, and filling your program with forms and controls, you’ll need to write the code that makes those controls and forms do something.

The Visual Basic language supports a large number of programming constructs and elements, and that language is the foundation on which we’ll build in this book. A good start here is essential for the work we’ll do throughout the book.

If you’ve programmed in other languages, much of the material in this chapter will probably be familiar to you—and once you understand the basics, you will be able to create powerful applications using Visual Basic.

How Does Visual Basic Code Look?

We’re going to take a look at the elements of the Visual Basic language that will let us make Visual Basic code work. What will that code look like? Some of our code will be short, such as when we check for multimedia device errors like this in Chapter 22:

Private Sub MMControl1_Done(NotifyCode As Integer)
    If MMControl1.Error <> 0 Then
        MsgBox MMControl1.ErrorMessage
    End If
End Sub

Some of our code will be a little longer, such as this code, where we display the status of a CD-ROM drive that’s playing a music CD:

Private Sub MMControl1_StatusUpdate()
    Dim strMode As String
    strMode = ""

    Select Case MMControl1.Mode
        Case mciModeReady
            strMode = "Ready."

        Case mciModeStop
            strMode = "Stopped."

        Case mciModeSeek
            strMode = "Seeking."

        Case mciModePlay
            strMode = "Playing."

        Case mciModeRecord
            strMode = "Recording."

        Case mciModePause
            strMode = "Paused."

    End Select

    Label1.Caption = strMode

End Sub

That’s what the Visual Basic language looks like at work. As you can imagine, knowing how to write the code is necessary to get anywhere in Visual Basic.

In the topics coming up, then, we’ll see how to declare variables, functions, and subroutines—and what those elements mean. We’ll see how to use text strings, conditionals, operators, loops, and math techniques. We’ll even see how to handle special Visual Basic formats like dates and financial data. And we’ll see some items that programmers like but don’t often encounter in programming books, such as how to use Switch() and Choose().

We’ll cover tasks that involve some complexity and whose syntax is hard to remember. In this way, this chapter also acts as a reference for easy lookup of those hard-to-remember items—and can save you from reinventing the wheel.

We’ll see a lot of syntax in this chapter, and there’s one convention you should be aware of before starting: we’ll use brackets for optional elements and keywords like this for the Dim statement:

Dim [WithEvents] varname [([subscripts])] [As [New] type] [, [WithEvents]
varname[([subscripts])] [As [New] type]]

Here, all the elements in square brackets are optional, and the variable names in italics are placeholders—you fill them in with the names of your variables as appropriate for your program

It’s time to turn to the Immediate Solutions now—no further introduction is needed.

Immediate Solutions

Declaring Constants

You’ve filled your code with numeric values—and now it’s time to change them all as you start work on the new version of the software. What a pain to have to track down and change all the numeric values (called magic numbers) throughout all the code. Isn’t there a better way?

There is: Use constants and declare them all in one place, then refer to the constants by name throughout the code instead of hardwiring numeric values in the code. When it’s time to change those values, you just change the constants, all in one well-defined part of the code.

How do you use constants? You declare constants in Visual Basic with the Const statement:

[Public | Private] Const constname [As type] = expression

The Public keyword is used at the module level to make a constant global. This keyword is not allowed in procedures. The Private keyword is used at the module or form level to declare constants that are private, which means only available within the module or form where the declaration is made. Like the Public keyword, Private is not allowed in procedures (constants in procedures are always private anyway). The constname identifier is the actual name of the constant. The type identifier is the data type of the constant, which may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String, or Variant. The expression identifier holds the value you want for this constant. It may be a literal, other constant, or any combination that includes all arithmetic or logical operators (except the Is operator).


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.