1. Technology
Send to a Friend via Email
VB.NET: What Happened to Control Arrays!!! (Part II)
VB.NET no longer supports VB 6 Control Arrays
Or does it? The Computing Studies Group disagrees.
The Computing Studies Group solution

Frank was kind enough to forward some feedback that he received from a Computing Studies Group that he is a member of when he asked the same question about control arrays.

In brief, people in Frank's group said, "Hey! You can have a control array in VB.NET! And it's a lot more simple than the 'Ugly' code sample in the article." And they provided an example. Frank said it was "elegant".

Welllllll ........ I'm not so sure. (And since I mainly quoted Microsoft in my article, I suppose I can claim that I have them on my side on this.) You can decide for yourself.

If you happened to find this article by searching and you haven't read Part I yet, here's the link for Part I of this article.


Frank's Study Group provided an example with a form that has 4 labels and 2 buttons. Button 1 clears the labels and Button 2 fills them. It's a good idea to read Frank's original question again and notice that the example he used was a loop that is used to clear the Caption property of an array of Label components. Here's the VB.NET equivalent of that VB 6 code. This code does what Frank originally asked for!

Public Class Form1
      Inherits System.Windows.Forms.Form
  
  #Region " Windows Form Designer generated code "
  
          Dim LabelArray(4) As Label
          'declare an array of labels
      Private Sub Form1_Load( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) _
          Handles MyBase.Load
          SetControlArray()
      End Sub
  
      Sub SetControlArray()
          LabelArray(1) = Label1
          LabelArray(2) = Label2
          LabelArray(3) = Label3
          LabelArray(4) = Label4
      End Sub
  
      Private Sub Button1_Click( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) _
          Handles Button1.Click
          'Button 1 Clear Array
          Dim a As Integer
          For a = 1 To 4
              LabelArray(a).Text = ""
          Next
      End Sub
  
      Private Sub Button2_Click( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) _
          Handles Button2.Click
          'Button 2 Fill  Array
          Dim a As Integer
          For a = 1 To 4
              LabelArray(a).Text = _
              "Control Array " & CStr(a)
          Next
      End Sub
  End Class

If you experiment with this code, you will discover that in addition to setting properties of the Labels, you can also call methods. So why did I (and Microsoft) go to all the trouble to build the "Ugly" code in Part I of the article?

I have to disagree that it's really a "Control Array" in the classic VB sense. The VB 6 Control Array is a supported part of the VB 6 syntax, not just a technique. In fact, maybe the way to describe this example is that it is an array of controls, not a Control Array.

In Part I, I complained that the Microsoft example ONLY worked at run time and not design time. You can add and delete controls from a form dynamically, but the whole thing has to be implemented in code. You can't drag and drop controls to create them like you can in VB 6. This example works mainly at design time and not at run time. You can't add and delete controls dynamically at run time. In a way, it's the complete opposite of the Part I example.

The classic VB 6 control array example is the same one that is implemented in the VB .NET code. Here in VB 6 code (this is taken from Mezick & Hillier, Visual Basic 6 Certification Exam Guide, p 206 - slightly modified, since the example in the book results in controls that can't be seen):

Dim MyTextBox as VB.TextBox
Static intNumber as Integer
intNumber = intNumber + 1

Set MyTextBox = _
	Me.Controls.Add("VB.TextBox", _
	"Text" & intNumber) 
MyTextBox.Text = MyTextBox.Name 
MyTextBox.Visible = True 
MyTextBox.Left = _
(intNumber - 1) * 1200

But as Microsoft (and I) agree, VB 6 control arrays aren't possible in VB.NET. So the best you can do is duplicate the functionality. My article duplicated the functionality found in the Mezick & Hillier example. The Study Group code duplicates the functionality of being able to set properties and call methods.

So the bottom line is that it really depends on what you want to do. VB.NET doesn't have the whole thing wrapped up as part of the language -- Yet -- but ultimately it's far more flexible.

Or ... at least that's the way I see it. As always ... I'd love hear your point of view.

About Visual Basic reader John Fannon also contributed a solution for VB.NET "control arrays" - this time using subroutine approach that is a bit easier to code than Microsoft's "official" position. You can read about John's suggestion in Part III of this article.

©2014 About.com. All rights reserved.