| 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): 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. 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 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. |
||

