VB Classic Flashcards Library

These FlashCards are contributed by you (our online community members). They are organized by our knowledge base topics. Specifically, by the VB Classic sub-topics.
|
35 Visual Basic Classic FlashCards
Group: Visual Basic Classic
Topic: VB Classic
VB Classic Array (x = Array())
|
Arrays in VB Classic use a 0-based indice. UBound returns -1 if the array has no elements, 0 if it has 1, 1 if it has 2, etc.
|
|
|
Dim MyArray As Variant Dim i As Integer MyArray = Array("Mike", "Lisa", "Felicia", "Nathan") If UBound(MyArray) > -1 Then For i = 0 To UBound(MyArray) MsgBox (MyArray(i)) Next End If
|
|
|
|
VB Classic Empty String Check (Len(s&vbNullString))
|
In VB Classic, you have to add an empty string to the value being compared in order to get consistent results. For example, add &"" to your string varilable or it's code equivalent &vbNullString. Then compare to an empty string or verify it's length to 0 with Len.
|
|
|
All these will work for variables unassigned, set to "", or set to Null: If s&"" = "" Then MsgBox ("Quotes with &'' say null is empty") End If If Len(s&"") = 0 Then MsgBox ("Len with &'' says null is empty") End If If Len(s&vbNullString) = 0 Then MsgBox ("Using vbNullString also works!") End If
|
|
|
|
|
Function SomeRoutine(ByRef pPerson, ByVal pName, Age)
|
|
|
|
Topic: Tool Basics
Tip: Mimic Short-Circuit Evalution in VB
|
Short-circuit evaluation is a feature of most languages where once an evaluation evaluates to False, the compiler evaluates the whole expression to False, exits and moves on to the next code execution line. In VB Classic, the if statement does not support short-circuit evaluation but you can mimic it. Use either an if..else if..else if statement or nested if statements. You will find that your code that makes use of this technique will be clearer and easier to maintain than the short-circuit equivalent and faster than ingnoring the issue.
|
|
|
|
|
|
Dim Age As Integer Dim FullName As String FullName = "Randy Spitz" Age = 38
|
|
|
|
VB Classic Case Sensitivity (No)
|
VB Classic is not case sensitive. If you type any other case for commands or variables, VB Classicwill change it to the "accepted" or "defined" case. For example, if you type msgbox it is converted to MsgBox.
|
|
|
The following code works: MsgBox ("hello")
|
|
|
|
|
Sub x End Sub If x Then End If While x WEnd
|
|
|
|
VB Classic Comments (' or REM)
|
Commenting Code VB Classic, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). VB Classic does NOT have a multiple line comment.
Directives - #
Directives are sometimes called compiler or preprocessor directives. A # is used for directives within VB Classic code. VB Classic offers only an #If..then/#ElseIf/#Else directive.
|
|
|
'Single line comment.
REM Old school single line comment. #If MyDirective Then '...some code. #End If
|
|
|
|
|
//Does VB evaluate the math correctly? No! If 0.1 + 0.1 + 0.1 = 0.3 Then MsgBox "correct" Else MsgBox "not correct" End If
|
|
|
|
|
Const kPI = 3.1459 Const kName = "Mike" //Public variable: Public Const kFeetToMeter=3.28, kMeterToFeet=.3
|
|
|
|
VB Classic Deployment Overview
|
VB applications require the VB runtime DLL (for version 6, it's VB600.DLL) plus any additional dependencies you've added such as Crystal Reports, ActiveX controls, and DLLs.
You can use any of the many free and commercially available installation packages.
|
|
|
|
VB Classic Development Tools
|
Languages Focus: Development ToolsPrimary development tool(s) used to develop and debug code.
VB Classic Development ToolsMicrosoft Visual Basic 1...6. VB Classic is not compatible with VB.Net.
|
|
|
|
VB Classic End of Statement (Return)
|
Languages Focus: End of StatementIn coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.
VB Classic End of StatementA return marks the end of a statement and you cannot combine statements on a single line of code. You can break a single statement into two or more code lines by using a space and underscore " _".
|
|
|
MsgBox "Hello1" MsgBox "Hello2" MsgBox "Hello3" 'The following commented code 'on a single line does not work... 'MsgBox "Hello4" MsgBox "Hello5" 'Two or more lines works too with a space+underscore: MsgBox _ "Hello6";
|
|
|
|
|
//Single line example. If X = True Then MsgBox "hello" //Complete example. If X = True Then MsgBox "hello" ElseIf Y = "ABC" Then MsgBox "goodbye" Else MsgBox "what?" End If
|
|
|
|
|
MsgBox ("Hello") MsgBox ("Hello ""Mike"".") 'Does VB evaluate this simple 'floating point math correctly? No! If (.1 + .1 + .1) = 0.3 Then MsgBox "Correct" Else MsgBox "Not correct" End If
|
|
|
|
VB Classic Overview and History
|
Language Overview: Class based language. Although you can create classes, VB Classic is not fully OOP. It is a traditional language with a few OOP extensions. You code in a traditional approach using functions, procedures, and global data, and you can make use of simple classes to help organize your reusable code. It also supports one-level abstract class to implemented class using the Implements keyword.
Target Platforms: Microsoft Visual Basic 6 is most suitable for creating Windows desktop applications that use the VB600.DLL runtime DLL within Microsoft Windows.
|
|
|
|
VB Classic Report Tools Overview
|
Crystal Reports was very popular with VB Classic developers and came bundled with Visual Basic 3 through 6. VB6 offers both Crystal Reports and the new Microsoft Data Report Designer.
|
|
|
|
VB Classic String Concatenation (& or +)
|
Although you can use either a & or a + to concatenate values, my preference is to use a + because more languages use it. However, if you use & then some type conversions are done for you. If you use + you will sometimes have to cast a value to concatenate it. For example, you will have to use CStr to cast a number to a string if you use the + operator as a concatenation operator.
|
|
|
Dim FirstName As String Dim LastName As String FirstName = "Mike" LastName = "Prestwood" MsgBox "Full name: " & FirstName & " " + LastName MsgBox "2+2=" + CStr(2+2)
|
|
|
|
VB Classic Variables (Dim x As Integer)
|
VB Classic is a loosely typed language. Declaring variables is optional unless you use the Option Explicit statement to force explicit declaration of all variables with Dim, Private, Public, or ReDim. Using Option Explicit is strongly recommended to avoid incorrectly typing an existing variable and to avoid any confusion about variable scope.
Undeclared variables are variants. To specifically declare a variant, use: Dim x As Variant Dim x
Common data types include Byte (0..255), Boolean, Integer (2-byte integers), Long (4-byte integers), Currency, Single (32-bit number), Double (64-bit number), Date, String, and variant.
Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.
|
|
|
Dim FullName As String Dim Age As Integer Dim Weight As Double FullName = "Mike Prestwood" Age = 32 Weight = 154.4 'Declaritive assignment not supported: ''Dim Married As String = "Y" '>>>Not supported.
|
|
|
|
Topic: Language Basics
|
'Given expressions a, b, c, and d: If Not (a and b) and (c or d) Then 'Do something. End If
|
|
|
|
Topic: Language Details
VB Classic Associative Array (Collection)
|
In addition to Add and Item, collections also offer Count and Remove. Notice that Add uses the format of Value, Key (which is backwards from many other languages).
|
|
|
Dim States As New Collection States.Add "California", "CA" States.Add "Nevada", "NV" MsgBox (States.Item("CA"))
|
|
|
|
VB Classic Class..Object (Yes)
|
One class per file. File must use a .cls extension (instead of .bas). You can include your classes in your project (a traditional OOP approach) or compile them to a DLL or ActiveX EXE, register them, and use them that way (a uniquely VB approach).
|
|
|
|
VB Classic Custom Routines (Sub, Function)
|
VB Classic is a non-OOP language with some OOP features. It offers both Subs and Functions. A Sub does not return a value while a Function does. When Subs and Functions are used in a class module, they become the methods of the class.
|
|
|
Sub DoSomething(ByVal pMessage As String) MsgBox (pMessage) End Sub Function GetAge(ByRef pFullname As String) As Integer GetAge = 7 End Function
|
|
|
|
VB Classic Self Keyword (Me)
|
The Me keyword is a built-in variable that refers to the class where the code is executing. For example, you can pass Me from one module to another.
|
|
|
Private Sub Command4_Click() MsgBox Me.Name 'Displays name of form (Form1 in this case). End Sub
|
|
|
|
Topic: Commands
|
Dim LeftString As String LeftString = Left("Prestwood", 3) MsgBox LeftString
|
|
|
|
Topic: OOP
VB Classic Constructors (Class_Initialize)
|
When an object instance is created from a class, VB6 calls a special parameter-less sub named Class_Initialize. Since you cannot specify parameters for this sub, you also cannot overload it.
When a class is destroyed, VB6 calls a special sub called Class_Terminate.
|
|
|
|
VB Classic Destructor
|
When an object instance is destroyed, VB6 calls a special parameter-less sub named Class_Terminate. For example, when the variable falls out of scope. Since you cannot specify parameters for this sub, you also cannot overload it.
When an object instance is created from a class, VB6 calls a special sub called Class_Initialize.
|
|
|
|
VB Classic Interfaces
|
VB6 has limited support for interfaces. You can create an interface of abstract methods and properties and then implement them in one or more descendant classes. It's a single level implementation though (you cannot inherit beyond that). The parent interface class is a pure abstract class (all methods and properites are abstract, you cannot implement any of them in the parent class).
In the single level descendant class, you have to implement all methods and properties and you cannot add any. Your first line of code is Implements InterfaceName.
|
|
|
|
VB Classic Member Method (sub, function)
|
VB classic uses the keywords sub and function. A sub does not return a value and a function does. Many programmers like to use the optional call keyword when calling a sub to indicate the call is to a procedure.
|
|
|
|
VB Classic Member Visibility
|
In VB Classic, the keywords Private, Friend, Public, and Static are used to set access levels for declared elements.
|
|
|
|
|
|