Sunday, August 23, 2009

Understanding and Optimizing Data Types In Visual Basic 6.0


In Visual Basic 6 there are 11 different data types. These are Boolean, Byte, Currency, Date, Double, Integer, Long, Object, Single, String, and Variant. They each have a specific purpose and using them correctly will increase your programs performance. I am going to cover the data types most frequently used.


• Boolean
The Boolean data type has only two states, True and False. These types of variables are stored as 16-bit (2 Byte) numbers, and are usually used for flags. For example, lets say that you have a textbox (Text1) and a command button (Command1). You only want Command1 to be Enabled when there is text in Text1. You would do something like this...



Private Sub Form_Load()
Command1.Enabled = False    ' Disables Command1
Text1.Text = vbNullString   ' Sets Text1=""
End Sub

Private Sub Text1_Change()
Dim bEnable As Boolean
If Text1.Text <> "" Then bEnable = True
Command1.Enabled = bEnable
End Sub

Run the program and Command1 will only be enabled when there is text typed into Text1.
• Byte

The Byte data type is an 8-bit variable which can store value from 0 to 255. This data type is very useful for storing binary data. It can also be very useful when sending/receiving byte values to/from a Basic Stamp or PIC.
• Double

The Double data type is a 64-bit floating point number used when high accuracy is needed. These variables can range from -1.79769313486232e308 to -4.94065645841247e-324 for negative values and from 4.94065645841247e-324 to 1.79769313486232e308 for positive values.
• Integer

The Integer data type is a 16-bit number which can range from -32768 to 32767. Integers should be used when you are working with values that can not contain fractional numbers.
• Long

The Long data type is a 32-bit number which can range from -2,147,483,648 to 2,147,483,647. Long variables can only contain non-fractional integer values. I myself use Long variables over Integers for increased performance. Most Win32 functions use this data type for this reason.
• Single

The Single data type is a 32-bit number ranging from -3.402823e38 to -1.401298e-45 for negative values and from 1.401298e-45 to 3.402823e38 for positive values. When you need fractional numbers within this range, this is the data type to use.
• String

The String data type is usually used as a variable-length type of variable. A variable-length string can contain up to approximately 2 billion characters. Each character has a value ranging from 0 to 255 based on the ASCII character set. Strings are used when Text is involved.
Putting All Of This Technical Stuff To Use

Just to show you how to use these data types, here is a small example. Lets say that we have a String containing the text, "This VB stuff is pretty darn cool..!", and we want to convert each letter to it's ASCII equivalent. We will then display each letter along with its ASCII equivalent in a MessageBox one at a time.


Private Sub Command1_Click()
Dim sText As String
Dim lTextLength As Long
Dim sChar As String
Dim bASCII As Byte
Dim x As Long

sText = "This VB stuff is pretty darn cool..!"
lTextLength = Len(sText) 'Gets # of chars in sText

For x = 1 To lTextLength 'Loop through string one char at a time
   sChar = Mid$(sText, x, 1)'Gets the x'th charcter in sText
   bASCII = Asc(sChar)      'Gets ASCII value of character
   MsgBox "The ASCII value of '" & sChar & "' is " & bASCII 'Display results
Next x
 
End Sub


Now run the code and it will display one character at a time along with it's ASCII value.

0 comments:

Your Ad Here