Referring to variable on different form, same project
Hi. I have the below code in a secondary form in a VB game I'm working on (in VB 6.0).
Code:
Private Sub Command3_Click()
If (Command3.Caption = "Continue") Then
beerscore = (Start.scoretotal + drink)
Start.lblScore.Caption = "hello"
Start.scoretotal = Start.scoretotal + drink
MsgBox "Your points have been deposited into your score."
End If
Beergame.Visible = False
Start.Visible = True
End Sub
'scoretotal' is a value on the first form. Am I referring to it correctly in this code? I'm attempting to add the value 'drink' from the current form to 'scoretotal' in the first form, and post that number in 'Start.lblScore' (a lable). The first form is called 'Start'. The one this code is on is called 'beergame'.
I hope that's clear enough, but ask for elaboration and I'll explain further. Thanks in advance for an help. :)
Re: Referring to variable on different form, same project
If Start is the name of the other form, then it looks right. Have you tested it? Do any errors occur?
Re: Referring to variable on different form, same project
Hi,
Like Mana said, it looks good to me assuming you have declared ScoreTotal as Public in the form Start like:
'It may be better suited in a module as opposed to a form.
Public ScoreTotal As Long 'whichever data type you are using.
Have a good one!
BK
Re: Referring to variable on different form, same project
Quote:
Originally Posted by Isolated
Hi. I have the below code in a secondary form in a VB game I'm working on (in VB 6.0).
Code:
Private Sub Command3_Click()
If (Command3.Caption = "Continue") Then
beerscore = (Start.scoretotal + drink)
Start.lblScore.Caption = "hello"
Start.scoretotal = Start.scoretotal + drink
MsgBox "Your points have been deposited into your score."
End If
Beergame.Visible = False
Start.Visible = True
End Sub
'scoretotal' is a value on the first form. Am I referring to it correctly in this code? I'm attempting to add the value 'drink' from the current form to 'scoretotal' in the first form, and post that number in 'Start.lblScore' (a lable). The first form is called 'Start'. The one this code is on is called 'beergame'.
I hope that's clear enough, but ask for elaboration and I'll explain further. Thanks in advance for an help. :)
Welcome to VB Forums!
You didn't say what problem you were experiencing.
Re: Referring to variable on different form, same project
Ahhh, I had 'scoretotal' as a 'Dim'. Changing it to 'Public' worked, with a couple of small changes. Thanks to everyone who replied!
Yea, by the way, I realized afterward that I hadn't actually asked a question. Heh.
Re: Referring to variable on different form, same project
It's a matter of scope. Something, no matter how much languages advance, it seems to remain as a basic concept for any language. Isolated, I profusely suggest you to invesitgate on the matter. You could start here. This is one of the things that will take you to the all-time coder's hall of fame.
BluEyes.