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

Join 132,398 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,236 people online right now. Registration is fast and FREE... Join Now!




Global Variables - How do I create one?

 
Reply to this topicStart new topic

Global Variables - How do I create one?, My counter variable keeps resetting - how do I make that not Happen?

steverthebeaver
post 30 Aug, 2008 - 02:07 PM
Post #1


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 1

Hello. I'm new to Visual Basic and I'm writing a program which have 10 images move on the screen. If an image is clicked one, it stops and if effectively "killed".

At the end of the game I want to move the score to a label and be done with the game.

Right now when the image is clicked on, the counter advances as it should. But later when another button (for quit game) is pressed the counter is not recognized and is zero.

I know I need to use a GLOBAL VARIABLE. I have tried the following.....

global penguinstopped as integer = 0 which produced a "compile error/synax error".

I tried public penguinstopped as integer which produced a "compile error - invalid attribute in Sub or Function".

I tried putting that code in the command1 button and also when the form is loaded - neither way works.

This can't be that hard - can anyone tell me what I am doing wrong????

Thanks in advance...


User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 30 Aug, 2008 - 03:28 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(steverthebeaver @ 30 Aug, 2008 - 06:07 PM) *

I know I need to use a GLOBAL VARIABLE.

Normally, I wouldn't be so blunt, but come on. This has got to be item #1 in ANY Visual Basic book. I'm sure a lengthy five seconds on Google could provide you this answer.

If you are having problems with your code, we'll gladly take a look at it. However, such a simple beginner/basic question as a Global variable...?

code.gif
User is offlineProfile CardPM

Go to the top of the page

Reverand Dave
post 30 Aug, 2008 - 06:05 PM
Post #3


D.I.C Regular

Group Icon
Joined: 27 Jul, 2008
Posts: 373



Thanked 2 times

Dream Kudos: 50
My Contributions


I agree that is kind of a rookie question, but since i need to boost my post count i'll take it and I don't want you to think no one is going to help you.
In vb, the global assignation is indicated by the tag "Public" If a variable is public then it is able to bne referenced by any function for subroutine in the the program.

Add your Public variable in the declaration section of your form as

vb

Option Explicit
Public penguinstopped as Integer


The go about assigning a value to it in any module or procedure such as:

vb

Sub Cmd1_Click()

penguinstopped = penguinstopped + 1

end sub


I hope this helps.

This post has been edited by Reverand Dave: 31 Aug, 2008 - 03:31 AM
User is offlineProfile CardPM

Go to the top of the page

Ken Halter
post 31 Aug, 2008 - 10:17 AM
Post #4


New D.I.C Head

*
Joined: 18 Nov, 2007
Posts: 35



Thanked 5 times
My Contributions


...adding to the pile here...

If you're coding in a version of VB that accepts...

global penguinstopped as integer = 0

... or any other assignment during declaration, you're in the wrong group! :-)

That would be a VB.Net question/answer
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 07:32AM

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