Results 1 to 11 of 11

Thread: [RESOLVED] VB6 How to declare an array as global.

  1. #1

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Resolved [RESOLVED] VB6 How to declare an array as global.

    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!
    Last edited by Flashbond; Jan 25th, 2013 at 06:49 AM.

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: VB6 How to declare a Dim as global.

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

  3. #3

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: VB6 How to declare a Dim as global.

    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;
    Code:
    Sub TxtBx(i As Integer)
        With Form1("TextBox" & i)
         ......
    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)
         MyArray(i - 1) = .Text
    Thanks for your consider.
    Last edited by Flashbond; Jan 25th, 2013 at 07:08 AM.

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: VB6 How to declare an array as global.

    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:
    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
    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:
    Form1.MyArray(0) = "hi"
    "Can't assign to read-only property" errors result.


    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.

  5. #5
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: VB6 How to declare an array as global.

    Knock up a small project, and attach it.
    And either here, or in comments in the program state what you want it to do.

  6. #6

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: VB6 How to declare an array as global.

    Thanks dilettante, I think that you are right. I deleted the module and moved the procedure into Form.

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: VB6 How to declare an array as global.

    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.

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: VB6 How to declare an array as global.

    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

  9. #9

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: VB6 How to declare an array as global.

    I am very new with VB and I don't know what is a control array nor how to use it.

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: VB6 How to declare an array as global.

    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
    Code:
    Private Sub Text1_Change()
    
    End Sub
    An event from a text box array would look like
    Code:
    Private Sub Text1_Change(Index As Integer)
    
    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.

  11. #11

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: VB6 How to declare an array as global.

    Aah! That's very clever. Thank you vey much for the tip

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width