Export (0) Print
Expand All

A Component in Visual Basic

.NET Framework 1.1

Here is the full source-code listing for the sample string component in Visual Basic:

Listing 1. Component in Visual Basic (CompVB.vb)

Option Explicit
Option Strict

Imports System

Namespace CompVB

   Public Class StringComponent

      Private StringSet(3) As String

      Public Sub New()
         MyBase.New
         StringSet(0) = "Visual Basic String 0"
         StringSet(1) = "Visual Basic String 1"
         StringSet(2) = "Visual Basic String 2"
         StringSet(3) = "Visual Basic String 3"
      End Sub

      Public Function GetString(ByVal index as Integer) 
         As String
         If ((index < 0) or (index >= Count)) then
            throw new IndexOutOfRangeException
         End If
         GetString = StringSet(index)
      End Function

      ReadOnly Property Count() As Long
         Get
            Count = StringSet.Length
         End Get
      End Property

   End Class

End Namespace

Like with Managed Extensions for C++ and Visual C#, both the namespace and the class name are specified in code.

This code also introduces the new Option Strict statement, which controls whether variable-type conversions are implicit or explicit. Implicit conversions can be done without any special syntax. Explicit conversions, on the other hand, must be done using the cast operators. If this option is turned on, only widening conversions — for example, from an Integer to a Double — can be done implicitly. The Option Strict functionality can also be activated at compile time, by using the /optionstrict+ compiler switch.

In Visual Basic, a class constructor is given the name New, rather than the name of the class as in the other languages. Because a constructor does not return a value, it is implemented in Visual Basic as a Sub rather than a Function:

Public Sub New()
...
End Sub

Also note this statement:

MyBase.New

This statement, which is required, calls the base-class constructor. In C++ and Managed Extensions for Visual C#, the call to the base-class constructor is automatically generated by the compiler.

Here is the GetString method, which takes an integer and returns a string (in Visual Basic, a subroutine that returns a value is called a function):

Public Function GetString(ByVal index as Integer) 
      As String
   If ((index < 0) or (index >= Count)) then
      throw new IndexOutOfRangeException
   End If
   GetString = StringSet(index)
End Function

The throw statement in the GetString method highlights the new, runtime-based exception handling:

throw new IndexOutOfRangeException

This statement creates — and throws — a new object of type IndexOutOfRangeException.

Note   Previous versions of the Visual Basic runtime implemented an Err.

Finally, you create the read-only Count property:

ReadOnly Property Count() As Long
   Get
      Count = StringSet.Length
   End Get
End Property

Building from the command line is quite simple. The only change is to write the component to the relative ..\Bin subdirectory, as shown here:

vbc.exe /t:library /debug+ /optionstrict+  /out:..\bin\CompVB.dll CompVB.vb

See Also

Clients for the Simple Components | Summary of Development Tutorial | Appendix A: Tools for Exploring Namespaces

Show:
© 2014 Microsoft