|
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
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, well 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, youll 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 well build in this book. A good start here is essential for the work well do throughout the book.
If youve programmed in other languages, much of the material in this chapter will probably be familiar to youand once you understand the basics, you will be able to create powerful applications using Visual Basic.
How Does Visual Basic Code Look?
Were 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 thats 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
Thats 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, well see how to declare variables, functions, and subroutinesand what those elements mean. Well see how to use text strings, conditionals, operators, loops, and math techniques. Well even see how to handle special Visual Basic formats like dates and financial data. And well see some items that programmers like but dont often encounter in programming books, such as how to use Switch() and Choose().
Well 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 itemsand can save you from reinventing the wheel.
Well see a lot of syntax in this chapter, and theres one convention you should be aware of before starting: well 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 placeholdersyou fill them in with the names of your variables as appropriate for your program
Its time to turn to the Immediate Solutions nowno further introduction is needed.
Immediate Solutions
Declaring Constants
Youve filled your code with numeric valuesand now its 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. Isnt 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 its 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).
|