Global Variables

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 162352 Views - Last Post: 25 August 2007 - 09:53 PM Rate Topic: -----

#1 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Global Variables

Posted 18 February 2005 - 09:57 AM

I am a java programmer trying to learn VB. Question! How do I declare a Global variable

Java is :

public static int num = 0

public static void main(String args[])
{
num
}

VB6:

Dim num As Integer = 0
this is where in need the help

I want to set num to a value out side my function

Thanks :D

Is This A Good Question/Topic? 0
  • +

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Global Variables

Posted 18 February 2005 - 10:09 AM

You set global variables in the general section of your code...it's the same process as other languages. I would be remiss as a coder, however, if I did not advise you to use as few global variables as possible.

Attached image(s)

  • Attached Image

Was This Post Helpful? 1

#11 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Global Variables

Posted 18 February 2005 - 10:24 AM

Right I understand that
Dim num As Integer

is the write way to declare a global variable, but I want to give num a value of zero(0) outside of the function
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Global Variables

Posted 18 February 2005 - 10:36 AM

The dim statement is the correct way to declare all variables, not just global ones. The fact that it's declared outside of the main sub is what makes it global.

Are you getting an error when you set it to 0?
Was This Post Helpful? 0
  • +
  • -

#13 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Global Variables

Posted 18 February 2005 - 10:45 AM

Yes! Invalid outside procedure Compile error:

this is my code

Private Declare Sub sleep Lib "kernel32" Alias "Sleep" _
(ByVal dwMilliseconds As Long)

Dim aryFile() As String
Dim fold1 As Folder

Dim num As Integer
Set num = 0

Dim fold2 As Folder

Dim fileColl2 As Files

Private Sub Class_Initialize()
getAllFiles ' gets all file from a path
getFileCount ' gets the number of files

Form1.Combo1.AddItem (num)
Form1.List1.Refresh
Form1.List1.AddItem aryFile(num)
sleep (1000)
num = num + 1

End Sub
Was This Post Helpful? 0
  • +
  • -

#14 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Global Variables

Posted 18 February 2005 - 10:46 AM

it is failing at the Set num = 0
Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Global Variables

Posted 18 February 2005 - 10:56 AM

try
num = 0


Was This Post Helpful? 0
  • +
  • -

#16 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Global Variables

Posted 18 February 2005 - 11:16 AM

num = 0 gives me the same error
Was This Post Helpful? 0
  • +
  • -

#17 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Global Variables

Posted 18 February 2005 - 11:24 AM

Let me explain what I am trying to do:

I have a Form with a button. Every time I hit the button I call a Function in the Class Module. The first time the Function is called num = 0 and the second time the Function is called num = 1 and, so on. I know in Java that a global static variable will handle this but, I have know clue on how to do it in VB
Was This Post Helpful? 0
  • +
  • -

#18 BlackJesus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 161
  • Joined: 10-February 05

Re: Global Variables

Posted 18 February 2005 - 11:20 PM

so basically all that you are doing is making the variable count up each time you click the command button right?
>_<
Was This Post Helpful? 1

#22 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Global Variables

Posted 19 February 2005 - 08:23 AM

Yes, but I believe he has that part taken care of already...his question is actually about the declaration of the global variable.

There are two ways to declare a global variable in VB: the old way was to declare them in the basic module with the keyword 'Global', ie:
Global num as Integer = 0


The preferred method now is to use the public/private keyword in the general declarations section
Public num as Integer = 0


On another note, are you declaring a sub within another sub? It may be that not all your code is posted, but I'd be surprised if that was not causing you problems.
Was This Post Helpful? 1

#23 BlackJesus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 161
  • Joined: 10-February 05

Re: Global Variables

Posted 19 February 2005 - 01:01 PM

well he could set the value when the form loads...i dont know a lot about modules, but couldnt you declare it somewhere in there? what else is you program supposed to do besides count the number of clicks? >_<
Was This Post Helpful? 0
  • +
  • -

#24 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Global Variables

Posted 19 February 2005 - 01:15 PM

You can think of modules as sections of the code...each function, or sub, can be considered a module, as can larger sections. The declaration of the a global variable needs to be outside any local procedures, so it will be in accessible by all procedures...you are correct, setting it when the form loads will make it global, unless there are multiple forms.
Was This Post Helpful? 0
  • +
  • -

#25 BlackJesus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 161
  • Joined: 10-February 05

Re: Global Variables

Posted 19 February 2005 - 01:18 PM

that is very true....i once used a module in vb and i did spmething like:

New Type blah
int x = 0
End Type

then in the form in the declarations you do:

Dim Whatever as blah

and you can use:

blah.x = 0

would that work?
Was This Post Helpful? 0
  • +
  • -

#26 BlackJesus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 161
  • Joined: 10-February 05

Re: Global Variables

Posted 19 February 2005 - 01:21 PM

on the previous post where i said

int x

that wouldnt be right (thats c++ and java...oops)

i think it would be

x as integer

then you declare it elsewhere
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2