Welcome to Dream.In.Code
Become a VB Expert!

Join 149,905 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,210 people online right now. Registration is fast and FREE... Join Now!




I need a Global Array in VB6

 
Reply to this topicStart new topic

I need a Global Array in VB6, Application requires an array to be updated by many different function

StanHelton
26 Dec, 2007 - 12:48 PM
Post #1

New D.I.C Head
*

Joined: 21 Oct, 2006
Posts: 6



Thanked: 2 times
My Contributions
I am testing several functions in VB6 for inclusion in a larger project. In order to test these functions before inserting them into the larger app, I have written some test code that requires a Global Array which can be updated from within each function. According to the docs, I should get a global array by DIMing it at the top of the module, but it doesn't seem to work. Below is a sample function and the code I'm using now.

CODE

DefLng A-Z
Dim GlobalArray(47) As Long
Dim NewGlobalArray(47) As Long

Public ShiftRight As Long

Public Function ShiftArray(ShiftRight As Long) As Boolean
    Dim i As Long
    Dim x As Long
        
    If ShiftRight = 0 Then
        ShiftArray = False
        Exit Function
    End If
    
    For i = 0 To 47
        If i <= 47 Then
            x = i + Shiftright
            If x > 47 Then
                Exit For
            Else
                NewGlobalStateArray(x) = GlobalArray(i)
            End If
        End If
    Next i
    
    ShiftArray = True

End Function


It this function returns True, I display both the GlobalArray and the NewGlobal array in a TextBox for verification. That part seems to be working so I didn't post it.

It seems that I'm always running into simple problems that should be obvious, but they are not obvious to me. Please help if you can.

Stan Helton

This post has been edited by StanHelton: 26 Dec, 2007 - 12:51 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: I Need A Global Array In VB6
26 Dec, 2007 - 04:18 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Change

CODE

Dim GlobalArray(47) As Long
Dim NewGlobalArray(47) As Long


to

CODE

Private GlobalArray(47) As Long
Private NewGlobalArray(47) As Long


And that should make the array global and it can be updated anywhere in that file, if you want it accessible to the entire application, then you need to put it into its own Class, or a Code Module (best idea in my pion)
User is online!Profile CardPM
+Quote Post

Nayana
RE: I Need A Global Array In VB6
26 Dec, 2007 - 05:31 PM
Post #3

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
I think he wants it to be accessible from other files, because using Dim at the top works just the same as using Private.

This will work
CODE

Public GlobalArray(47) As Long
Public NewGlobalArray(47) As Long


It doesn't really matter (in VB) whether you give it it's own object. An instance of a form is really a psuedo (IMHO) object thingy anyway.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: I Need A Global Array In VB6
26 Dec, 2007 - 05:45 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
If he wants it accessible from other modules he needs to put it in a code module, not cause users to constantly call a class file simply because there may or may not be a function in it they need.

Using a Code Module and making them Public will give him the desired results. You way could/would cause unnecessary resource usage by the system if there was nothing else in that class they needed it would require instantiating an object it doesn't need, causing more Garbage Collection sessions that aren't exactly needed, etc.
User is online!Profile CardPM
+Quote Post

Nayana
RE: I Need A Global Array In VB6
26 Dec, 2007 - 06:33 PM
Post #5

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
QUOTE(PsychoCoder @ 26 Dec, 2007 - 06:45 PM) *

If he wants it accessible from other modules he needs to put it in a code module, not cause users to constantly call a class file simply because there may or may not be a function in it they need.

Using a Code Module and making them Public will give him the desired results. You way could/would cause unnecessary resource usage by the system if there was nothing else in that class they needed it would require instantiating an object it doesn't need, causing more Garbage Collection sessions that aren't exactly needed, etc.


What? VB6 doesn't have garbage collection, it does reference counting... It also doesn't have proper objects.
If I were to do a global variable I would do what you say though. Just have it as Public in a BAS module.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 01:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month