Hi guys!
I want to declare a dim as global. But VB6 clearly warns me arrays cannot be declared as Public. Any trick to overcome this problem?
Thanks a lot!
Printable View
Hi guys!
I want to declare a dim as global. But VB6 clearly warns me arrays cannot be declared as Public. Any trick to overcome this problem?
Thanks a lot!
Hmm, can you give a short example of what fails?
Basically Dim should only be used for local data (within a procedure), just like Static (which only works within a procedure). Everything else should be Private, Public, or Friend depending on the situation. Global is almost identical to Public, but is obsolete and only there to help port ancient versions to VB6.
The rules also vary a bit depending on the module type (Form, Class, UserControl, static BAS module, etc.).
Sorry, dilettante. It was an array, I corrected my post as well.
My example is, I have an array with 20 elements (19) . In my form I have 20 TextBoxes and each has some formatting and function properties so I decided that it will not be practicable to write a change procedure for every single textbox and I added another module to hold all these actions. It is starting like;
One of the actions that I want is to send the entered text to the related array number which was previously declared in the Form1 to be used later by the form. I mean, I declared the array already in the main form but in the Module1 I need a line something like;Code:Sub TxtBx(i As Integer)
With Form1("TextBox" & i)
......
Thanks for your consider.Code:Sub TxtBx(i As Integer)
With Form1("TextBox" & i)
MyArray(i - 1) = .Text
Declaring data as Public in a Form means you are creating a Property on that Form, using abbreviated syntax. A Property cannot be an array using that shortcut syntax.
To say this another way, "Public" only means "global" for old-fashioned static (BAS) modules. For everything else Public means something entirely different.
So instead you might try to use:
But that's going to be awful, copying the entire array back and forth. And it also doesn't give what you expect, i.e. this will fail:Code:Private mMyArray(19) As String
Public Property Get MyArray() As String()
MyArray = mMyArray
End Property
Public Property Let MyArray(ByRef RHS() As String)
mMyArray = RHS
End Property
"Can't assign to read-only property" errors result.Code:Form1.MyArray(0) = "hi"
Instead you'd want something more like:
Code:Private mMyArray(19) As String
Public Property Get MyArray(ByVal Index As Long) As String
MyArray = mMyArray(Index)
End Property
Public Property Let MyArray(ByVal Index As Long, ByVal RHS As String)
mMyArray(Index) = RHS
End Property
"To Each His Own, I Guess" Department
But all of this feels a bit bizarre. You're doing some weird stuff there. Just to start with you should probably use a control array instead of the slow use of the Form's Controls collection as you are doing.
And moving UI-management or validation code out of a Form and into a static module seems strange at best. if anything should be moved I'd think it would be any non-GUI code, and that probably belongs in Class modules instead of costly static modules.
Knock up a small project, and attach it.
And either here, or in comments in the program state what you want it to do.
Thanks dilettante, I think that you are right. I deleted the module and moved the procedure into Form.
Well a lot of programs that are invented as they're written can get more twisted than we'd like. Sometimes going back and reworking them to be more streamlined can feel like a lot of effort.
Hmm I'm not really sure what the goal is here. Normally anything I would want to be global I would declare in a Module rather than a form and there is no problem in doing so if that is what you need.
That said in a later post there is a mention of textboxes so it sounds like it may make more since to create a usercontrol that consists of a customized text box with additional properties or methods as needed and then simply use that textbox instead of the default text box. Should be no need for a public array but you may want to use a control array of the custom textboxes
I am very new with VB and I don't know what is a control array nor how to use it.
Control arrays are simple in Vb
You simply add a control to your form, right click on it choose copy and then paste onto the form. VB will ask if you want to create a control array and if you say yes you have a control array with two controls in it Text1(0) and Text1(1) for example You can keep pasting additional text boxes and the index value will increase.
When using a control array all controls in the array share the same sub routines for event handlers and pass an Index to the event in order to identify which one triggered the event.
An event from a normal text box would look like
An event from a text box array would look likeCode:Private Sub Text1_Change()
End Sub
In the case of an array you would typically add a Select Case statement to test the index value or an If statement or maybe not depending on what you need it to do.Code:Private Sub Text1_Change(Index As Integer)
End Sub
Aah! That's very clever. Thank you vey much for the tip :)