We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,163 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Visual Basic carry a variable throughout an entire project

So I'm currently making program for my year 12 IPT assignment. It's a maths game for Year 4's and 5's.
I need to carry a variable throughout the entire project (across forms) so I can easily specify what level the user is playing and how many questions they want to answer etc..

The only way I would ever know how to do this would be to put different captions on labels and then read off the labels and I really don't want to have to do that. There's probably an advanced way that I've never heard of, but becuase my teacher doesn't know what he's doing my entire class is left to learn how to use VB by themselves and through books....so it would be great if I could get some help.

Thanks. Hopefully I've provided enough information.

It's really just one word that I think I need though...like " public level as string" or "static level as string" something like that...

4
Contributors
4
Replies
1 Day
Discussion Span
11 Months Ago
Last Updated
5
Views
Question
Answered
Parloverde
Newbie Poster
2 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Define it outside (before) any functions or subroutines.
(these are called "global variables.")
"public level as string"
looks correct.

If you need persistence between executions, set up a routine to write the global variables' values to a file at termination and another to read them from a file at initialization.

Good luck.

hkwhitten
Newbie Poster
8 posts since Oct 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

Also, you have to put actual global variables in a Module, not in a Form. Your best bet is to alter your project to use Startup Object "Sub Main" rather than a form. To do that, create a module, put in a subroutine named "main" that loads your initial form, then put your globals in front of that. In the Project Explorer, right click on your project (top level of the project tree) and select "Project1 Properties...". Then select the module from the Startup Object drop-down in the General tab.

Just to get you going, here's some sample code (assumes a Module named Module1 and a form named Form1):

' This is in Module1
Public myString As String
Sub main()
myString = "Joe"
Dim myForm As Form
Set myForm = New Form1
myForm.Show
End Sub

' This is in Form1
Private Sub Form_Load()
Me.Caption = myString
End Sub

Good luck!

BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14

How about using Property GET/Let?

Put this code in your form (General Declaration Section)

' Variable to hold 'VarValue' property value
Private refVarValue As String

Public Property Get VarValue() As String
   VarValue = refVarValue
End Property

Public Property Let VarValue(ByVal strValue As String)
   refVarValue = EnuValue
End Property

Then, when you call the form where you put the code above (ex, Form1)

Form1.VarValue = "Your String goes here"
Form1.Show

'Then on form1 load event, put this code
Private Sub Form_Load()
   Debug.Print refVarValue
End Sub

Try to run it and see what happens.

jhai_salvador
Posting Whiz in Training
226 posts since Mar 2008
Reputation Points: 58
Solved Threads: 39
Skill Endorsements: 2
Question Answered as of 11 Months Ago by jhai_salvador, hkwhitten and BitBlt

Thanks so much for the help everyone, the problem is solved! :D

Parloverde
Newbie Poster
2 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0659 seconds using 2.65MB