Previous Topic

Next Topic

Book Contents

Book Index

MS Visual Basic Sample Code

A sample Visual Basic Project file is included with the installation. We encourage users to reference that file.

There are two ways to use the COM interface from Microsoft Visual Basic

The first technique is to use Project ... References in the VB Editor to add a reference to the Random Numbers.tlb file located in the program folder. This will cause the class "RandomNumber.Generator" to become a class available in the Object Browser and through AutoComplete in the editor.

The second technique is more generic. It involves declaring an object variable and using CreateObject("RandomNumbers.Generator") to create the Generator object prior to calling methods.

The sample Visual Basic code below illustrates the first technique:

Private Sub cmdTest_Click()

' Uses the type library.

Dim objGenerator As RandomNumbers.Generator

Dim vResult As Variant

Dim lResult As Long

Dim dResult As Double

Dim Counter As Long

' Create the object.

Set objGenerator = New RandomNumbers.Generator

' Get buffer status.

lResult = objGenerator.BufferedData

dResult = objGenerator.BufferStatus

' Display

Label1.Caption = "Buffered data: " & lResult & _

" bytes, Buffer usage: " & dResult * 100 & "%"

' Retrieve Random numbers

lResult = objGenerator.RandomInteger(100)

dResult = objGenerator.RandomDouble

' Test for errors and display.

If lResult <> -1 And dResult <> -1 Then

Label2.Caption = "Random number 0 to 100: " & _

lResult & vbCrLf & "Random double 0 to 1: " & _

dResult

Else

' Show error.

Label2.Caption = "Error."

End If

' Retrieve Binary Data.

vResult = objGenerator.RandomData(100)

' Test result.

If IsEmpty(vResult) = False Then

' Reset display.

Label3.Caption = "Random Bytes: "

' Loop through data and display

For Counter = 0 To UBound(vResult)

Label3.Caption = Label3.Caption & _

" " & Hex(vResult(Counter))

Next

Else

Label3.Caption = "Error"

End If

End Sub

See Also

Using the COM Interface

MS Excel VBA Sample Code

MS Visual C++ Sample Code

VBScript Web Password Sample Code