VB.NET: What Happened to Control Arrays!!! (Part I) |
||
VB.NET no longer supports VB 6 Control Arrays
|
||
Frank Bugeja, About Visual Basic reader from Australia ... <G'day Mate!> ... writes, I am a senior computing studies teacher teaching software design and development. I have been using Visual Basic 6. Despite the fact that online experts of VB.NET and authors of VB.NET textbooks are saying that the omission of control arrays in VB.NET is a positive step, the omission of control arrays from VB.NET is, for educational purposes in teaching about arrays, a decidedly backward step. Why?
For example, to clear a control array of 6 labels of text in VB6, it is as simple as this: For a = 1 to 6 Label(a).Caption = "" Next Could you please show me how I can create a "control array" structure in VB.NET, and how it would be coded (using labels as the controls). |
||
|
Well, mate ... Pull your swag up closer to the bush telly and we'll have a go at it! In the style of a Clint Eastwood spaghetti western, I'm going to give you the Good, the Bad, and the really, really UGLY about this. And I do mean Ugly. We're talking "kiss your maiden aunt through an inch of pancake makeup" ugly here. In reverse order ... You're absolutely right! Well ... You're mainly right. If you reference the VB6 compatibility library, there are objects in there that act pretty much like control arrays. To see what I mean, simply use the VB.NET upgrade wizard with a program that contains a control array. The code is back to uh-uh-ugly again, but it works. The bad news is that Microsoft will not guarantee that the compatibility components will continue to be supported and you're not supposed to use them. The VB.NET code to create and use "control arrays" is much longer and much more complex. But you don't have to buy a book to see that. You just have to check out Microsoft's white paper about it: Creating Control Arrays in Visual Basic .NET. Microsoft heard this complaint quite a bit so they wrote this official response. The code in the white paper is for both C# and VB.NET and also uses buttons rather than labels as you requested. So here's my (hopefully) simplified version of the white paper using labels. According to Microsoft, to do something even close to what you can do in VB 6 requires the creation a "simple component that duplicates control array functionality". I'll let you be the judge ... but it doesn't look like a duplicate to me. And it's nothing close to being as simple. First the code. To illustrate how this class code would be used, you could create a Form that calls it. You would have to use the code shown below in the form: First, this doesn't even do the job at Design Time like we used to do it in VB 6! And second, they aren't in an array, they are in a VB.NET Collection - a much different thing than an array. Microsoft has the nerve lie to all of us by calling this a "duplicate"! Somehow ... I think that the marketing people at Microsoft are really to blame for problems like this. They are the ones who try to keep the fiction alive that VB.NET and VB 6 are anything more than distantly related. If you just accept the fact that VB.NET is a whole new language ... with great new features! ... then you can understand things a lot better. Regular readers of this site know that I'm a real advocate of VB.NET. I think it's a great language and is destined to dominate the development landscape more than VB 6 ever did. The reason VB.NET doesn't support the VB 6 "Control Array" is that there is no such thing as a "Control" "Array" (note the change of quotation marks). VB 6 creates a collection "behind the scenes" and makes it appear as an array to the developer. But it's not an array and you have little control over it beyond the functions provided through the IDE. VB.NET, on the other hand, calls it what it is: a collection of objects. And they hand the keys to the kingdom to the developer by creating the whole thing right out in the open. As an example of the kind of advantages this gives the developer, in VB 6 the controls had to be of the same type, and they had to have the same name. Since these are just objects in VB.NET, you can make them different types and give them different names and still manage them in the same collection of objects. In this example, the same Click event handles two buttons and a checkbox and displays which one was clicked. Do that in one line of code with VB 6! The substring calculation is kind of complex, but it isn't really what we're talking about here. You could do anything in the Click event. You could, for example, use the Type of the control in an If statement to do different things for different controls. Indications are that the Whidbey upgrade to VB.NET will have CONTROL ARRAYS back again. Since it hasn't been released yet, nobody but Bill knows for certain ... but it's a fairly good bet right now. After this article was completed and published, Frank forwarded a different point of view about VB.NET and control arrays to me. It's interesting ... but I don't completely agree. You can read all about it in what is now Part II of this article. And a few weeks further on, About Visual Basic reader John Fannon contributed one more 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. |
||
