Welcome to Dream.In.Code
Getting VB Help is Easy!

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




Global Variables

2 Pages V  1 2 >  
Reply to this topicStart new topic

Global Variables

zakary
post 18 Feb, 2005 - 08:57 AM
Post #1


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 400



Thanked 6 times

Dream Kudos: 175
My Contributions


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 biggrin.gif
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 18 Feb, 2005 - 09:09 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


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 thumbnail(s)
Attached Image
User is online!Profile CardPM

Go to the top of the page

zakary
post 18 Feb, 2005 - 09:24 AM
Post #3


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 400



Thanked 6 times

Dream Kudos: 175
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 18 Feb, 2005 - 09:36 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


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?
User is online!Profile CardPM

Go to the top of the page

zakary
post 18 Feb, 2005 - 09:45 AM
Post #5


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 400



Thanked 6 times

Dream Kudos: 175
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

zakary
post 18 Feb, 2005 - 09:46 AM
Post #6


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 400



Thanked 6 times

Dream Kudos: 175
My Contributions


it is failing at the Set num = 0
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 18 Feb, 2005 - 09:56 AM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


try
CODE

num = 0
User is online!Profile CardPM

Go to the top of the page

zakary
post 18 Feb, 2005 - 10:16 AM
Post #8


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 400



Thanked 6 times

Dream Kudos: 175
My Contributions


num = 0 gives me the same error
User is offlineProfile CardPM

Go to the top of the page

zakary
post 18 Feb, 2005 - 10:24 AM
Post #9


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 400



Thanked 6 times

Dream Kudos: 175
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

BlackJesus
post 18 Feb, 2005 - 10:20 PM
Post #10


D.I.C Head

Group Icon
Joined: 10 Feb, 2005
Posts: 161



Thanked 1 times

Dream Kudos: 175
My Contributions


so basically all that you are doing is making the variable count up each time you click the command button right?
>_<
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 19 Feb, 2005 - 07:23 AM
Post #11


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


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:
CODE

Global num as Integer = 0

The preferred method now is to use the public/private keyword in the general declarations section
CODE

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.
User is online!Profile CardPM

Go to the top of the page

BlackJesus
post 19 Feb, 2005 - 12:01 PM
Post #12


D.I.C Head

Group Icon
Joined: 10 Feb, 2005
Posts: 161



Thanked 1 times

Dream Kudos: 175
My Contributions


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? >_<
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 08:51AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month