Miscellaneous

Any tips for converting from VB 3 to VB 4?

Visual Basic Version: VB 4.0

Issue: Moving from VB 3 to VB 4 is relatively painless for simple applications. But with large or complex applications there are many incompatibilities between VB 3 and VB 4, especially in moving from the Win 16 API to the Win 32 API. But there is help available.

Crescent software is giving away a "Jump Start Kit" which includes a Crescent Upgrade Wizard application and a Survival Guide document. You can download them from the Crescent Jump Start Kit site. We used this for one of our projects and we were pleased with the results.


>How do I pass an array to a subroutine?

Visual Basic Version: VB3.0, VB 4.0, VB 5.0, VB 6.0

Issue: I can't seem to get the syntax for passing an array to a subroutine. How do I do it?

The trick is in the parenthesis. Pass a parameter that defines the array as shown in the code example below.

Code:

The following is the code for the Copy:

' Passes an array to the Concat function
Option Explicit
Dim x(4) As String

Private Sub Form_Load()
Dim result As String
    x(0) = "A"
    x(1) = "B"
    x(2) = "C"
    result = Concat(x)
    MsgBox result
End Sub

Function Concat(a() As String) As String
Dim temp As String
Dim i As Integer
    For i = 0 To UBound(a)
        temp = temp & a(i)
    Next i
    Concat = temp
End Function

How do I work with the Clipboard?

Visual Basic Version: VB 4.0, VB 5.0, VB 6.0

This code will also work with VB 3.0 if you take out references to the Err object.

Issue: To develop a production-quality application, the application should provide an Edit menu with Cut, Copy, and Paste options. These options should Cut text from a form and place it on the Clipboard, Copy text from a form and place it on the Clipboard, or Paste text from the Clipboard to the form.

Limitation: If you plan to support an Undo using the API undo facility, you need to user the API calls to work with the clipboard instead of using the Visual Basic Clipboard object.

Code:

The following is the code for the Copy:

' Constants for error handling
Const iCB_CLEAR_ERR = 3 + vbObjectError + 512
Const iCB_SET_ERR = 4 + vbObjectError + 512
Const iCB_PASTE_ERR = 5 + vbObjectError + 512

' Standard Copy procedure
' This could be enhanced to support alternative clipboard types
Public Sub EditCopy()
' Set the error handler
On Error Resume Next

' Clear the clipboard
Clipboard.Clear
If Err.Number Then
   Err.Raise iCB_CLEAR_ERR, "CApplication", "Could not clear clipboard."
Else
   ' Place selected text on the clipboard
   Clipboard.SetText Screen.ActiveControl.SelText
   If Err.Number Then
	Err.Raise iCB_SET_ERR, "CApplication", "Could not set text from active control to clipboard."
   End If
End If
End Sub

The following is the code for the Cut:

' Standard Cut procedure
' This could be enhanced to support alternative clipboard types
Public Sub EditCut()
' Set the error handler
On Error Resume Next

' Clear the clipboard
Clipboard.Clear
If Err.Number Then
   Err.Raise iCB_CLEAR_ERR, "CApplication", "Could not clear clipboard."
Else
   ' Place selected text on the clipboard
   Clipboard.SetText Screen.ActiveControl.SelText
   Screen.ActiveControl.SelText = ""
   If Err.Number Then
	Err.Raise iCB_SET_ERR, "CApplication", "Could not set text from active control to clipboard."
   End If
End If

End Sub

The following is the code for the Paste:

' Standard Paste procedure
' This could be enhanced to support alternative clipboard types
Public Sub EditPaste()
' Set the error handler
On Error Resume Next

' Place the text from the clipboard
Screen.ActiveControl.SelText = Clipboard.GetText()
If Err.Number Then
   Err.Raise iCB_PASTE_ERR, "CApplication", "Could not paste text from clipboard to active control."
End If

End Sub

When can I use the "!" operator?

Visual Basic Version: VB 4.0, VB 5.0, VB 6.0

Issue: The "!" operator is a shortcut for a keyed lookup in a collection provided by the Item method. So anytime you would code: x.Item("keystring") you could instead use: x!Keystring.

Limitations: This will not work with collection classes because the collection is Private (but it will work within the collection class). It will also not work using the index instead of the string key for the collection. i.e. x!1 generates a syntax error.

Code:

The following two lines provide the same results:

EmployeeList.Item("E00001").Name
EmployeeList.m_colEmployees!E00001.Name

In the example above, I had a collection class called EmployeeList. In it I have a collection called m_colEmployees. I also have methods and properties that wrap around the methods and properties exposed by the collection. So my EmployeeList collection class has an Item method that returns the same value as the collection Item method.


How do I convert a string to an array of bytes?

Visual Basic Version: VB 4.0, VB 5.0, VB 6.0

Issue: I need to convert a string into an array of bytes. How do I do that?

Author: This tip was provided by Greg DiGiorgio

Here is the code for the conversion:

Dim Str as String
Dim CharArray() as Byte
Dim i as integer
	Str = "My test string"
	ReDim CharArray(Len(Str))
	For i = 1 to Len(Str)
		CharArray(i) = Asc(Mid$(Str,i,1))
	Next i

Str is the beginning string and CharArray is the resulting array of bytes.

All contents © 2004 InStep Technologies, Inc. All rights reserved.