views:

378

answers:

3

With a view to avoiding the construction of further barriers to migration whilst enhancing an existing vb6 program. Is there a way to achieve the same functionality as control arrays in vb6 without using them?

A: 

Well, you could always create your own array of controls in code :) Perhaps a better container, though, is a Collection or Dictionary object. Depending on what you want to do, you could perhaps create a wrapper class for the control with a custom collection class... but creating an object model is far nicer using generics in .NET so probably best to keep it simple in VB6 for now.

VBA Userforms lack support for control arrays, so why not Google for suggestions on how to mimic control arrays with VBA, Userforms, Excel, etc.

BTW have you tried migrating control arrays from VB6 to VB.NET? Just a guess but considering they are commonly used in VB I imagine they are handled quite well.

onedaywhen
+1  A: 

In .NET you have a tag property. You can also have the same delegate handle events raised by multiple controls. Set the Tag property of the new control to the Index.

Private Sub MyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,Button2.Click

        Dim Btn As Button = CType(sender, Button)
        Dim Index As Integer = CType(Btn.Tag, Integer)
' Do whatever you were doing in VB6 with the Index property

End Sub

You also should look at the classes that inherit from BaseControlArray in the VB6.Compatibility which automates some of the work. I find the use of Tag to be less error prone in the conversion process than relying on the control name. However don't thank this as an absolute. You will have to decide whether the control name approach is best or the tag as index approach.

In either case you can easily setup .NET to funnel the events raised by multiple controls into one handler.

RS Conley
I think your point is that control arrays in vb6 aren't a problemthat needs to be addressed prior to migration?
kjack
You have to be aware of it when you do the migration as there is a lot of potential for subtle errors as you have to do everything manually.
RS Conley
A: 

I did a bit of reading and experimentation over the last couple of days and it seems that there is no other way in vb6 to be able to do the things control arrays do. If you already know the number of controls you'll be creating at runtime, at design time, then you can declare Private control object variables 'with events' and instance these at dynamically at runtime. If you need to create any more then you can do so but these won't have any code to fire in response to events. This as far as I can see is the nub of the problem. there is no way to dynamically associate code with an event of a dynamically created control in vb6.

kjack
correct. .NET has much richer API in this regard.
RS Conley