1. Home
  2. Computing & Technology
  3. Visual Basic
New Upgrade Wizard Tricks
Part 3: Converting a Control Array: The Wizard Way

One of the Control Arrays is a ten element array of command buttons -- Number() -- used to input the individual digits on the calculator keypad. In the original VB 6 code, it's handled this way (Number() is in bold type to help you find it):

Private Sub Number_Click(Index As Integer)
    If LastInput <> "NUMS" Then
        Readout = Format(0, ".")
        DecimalFlag = False
    End If
    If DecimalFlag Then
        Readout = Readout + Number(Index).Caption
    Else
        Readout = _
        Left(Readout, _
        InStr(Readout, Format(0, ".")) - 1) + _
        Number(Index).Caption + Format(0, ".")
    End If
    If LastInput = "NEG" Then _
        Readout = "-" & Readout
    LastInput = "NUMS"
End Sub

The code above shows that VB 6 handles the problem clearly and easily. Index identifies the button that was pushed but the actual value is taken right from the Caption property. The Upgrade Wizard creates almost the same thing for VB .NET.

Private Sub Number_Click( _
    ByVal eventSender As System.Object, _
    ByVal eventArgs As System.EventArgs) _
    Handles Number.Click
    Dim Index As Short = _
        Number.GetIndex(eventSender)
    If LastInput <> "NUMS" Then
        Readout.Text = VB6.Format(0, ".")
        DecimalFlag = False
    End If
    If DecimalFlag Then
        Readout.Text = _
        Readout.Text & Number(Index).Text
    Else
        Readout.Text = _
        VB.Left(Readout.Text, _
        InStr(Readout.Text, VB6.Format(0, ".")) - 1) & _
        Number(Index).Text & VB6.Format(0, ".")
    End If
    If LastInput = "NEG" Then _
        Readout.Text = "-" & Readout.Text
    LastInput = "NUMS"
End Sub

Index is no longer passed directly as an argument, but rather is derived using a method, GetIndex, of the Number object called with a different argument, eventSender.

Hmmm ... How convenient is it that VB .NET already has a Number object which was a local name for the Control Array in the VB 6 version? Answer: a little too convenient. Something else must be happening. Checking the hidden Region code at the top (see the About Visual Basic article about the Region), we find this interesting declaration:

Public WithEvents Number As Microsoft.VisualBasic.Compatibility.VB6.ButtonArray

ButtonArray Object

So Number is actually an instantiation of another object, ButtonArray, in the Compatibility.VB6 namespace. Let's take a closer look at this namespace. We can display more information by right-clicking and selecting Go to Definition. The definition of the Compatibility.VB6 namespace is shown at left.

Ah HAH! That's how VB .NET does it! It cheats and uses a library of objects that simulate every one of the possible VB 6 control arrays.

The reason this is important is that you might want to use the VB 6 control array technique in your own VB .NET program some day. Checking to see how the Upgrade Wizard does it is one way to find out how. Unfortunately, Microsoft doesn't recommend that you copy their methods on this one. The documentation has this to say about it.

It is not recommended that you use the VisualBasic.Compatibility namespace for new development in Visual Basic .NET. This namespace may not be supported in future versions of Visual Basic.

Leaving aside the question of whether Microsoft will support programs converted to VB .NET using their own Upgrade Wizard in the future (But you have to admit that it's an interesting question!), lets see if we can find another way to do this.

Next page > Converting a Control Array: A Better Way > Page 1, 2, 3, 4
Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.