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...

Dani AI

Generated

Good question, and nice progress so far. Building on and : in VB6 the clean way to share state across forms is a BAS module, not label captions and not form-level Public fields. Also note that Static only preserves a variable inside a single procedure; it does not make it project-wide.

A simple, robust pattern is to keep a single game state record and initialize it from Sub Main. Forms read and update that record, not each other.

' Globals.bas
Option Explicit

Public Type TGameState
    Level As Integer
    QuestionCount As Integer
End Type

Public Game As TGameState

Public Sub InitGame(Optional ByVal defaultLevel As Integer = 1, _
                    Optional ByVal defaultCount As Integer = 10)
    Game.Level = defaultLevel
    Game.QuestionCount = defaultCount
End Sub
' In Sub Main (Module)
InitGame 1, 20
Load frmMenu
frmMenu.Show
' In any form
If Game.Level = 2 Then
    ' do medium difficulty work here
End If

If you want the settings to persist between runs, use VB’s built-in registry helpers rather than rolling your own file I/O:

' Save on shutdown
SaveSetting "MathGame", "Options", "Level", CStr(Game.Level)
SaveSetting "MathGame", "Options", "QuestionCount", CStr(Game.QuestionCount)

' Load on startup (e.g., inside InitGame)
Game.Level = CInt(GetSetting("MathGame", "Options", "Level", "1"))
Game.QuestionCount = CInt(GetSetting("MathGame", "Options", "QuestionCount", "10"))

Tip: define an Enum for readability (e.g., Public Enum DifficultyLevel ... End Enum) and type Game.Level as that Enum. And ’s Property approach is great for exposing form-owned values; just fix the setter typo so the Let assigns strValue, not EnuValue. This structure keeps your Year 4/5 maths game predictable, testable, and easy to extend.

Recommended Answers

All 4 Replies

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.

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!

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.