I need a Global Array in VB6

Application requires an array to be updated by many different function

Page 1 of 1

4 Replies - 16217 Views - Last Post: 26 December 2007 - 07:33 PM Rate Topic: -----

#1 StanHelton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 21-October 06

I need a Global Array in VB6

Posted 26 December 2007 - 01:48 PM

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.

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 December 2007 - 01:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I need a Global Array in VB6

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: I need a Global Array in VB6

Posted 26 December 2007 - 05:18 PM

Change

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



to

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)
Was This Post Helpful? 0
  • +
  • -

#3 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: I need a Global Array in VB6

Posted 26 December 2007 - 06:31 PM

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
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.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: I need a Global Array in VB6

Posted 26 December 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.
Was This Post Helpful? 0
  • +
  • -

#5 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: I need a Global Array in VB6

Posted 26 December 2007 - 07:33 PM

View PostPsychoCoder, on 26 Dec, 2007 - 06:45 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1