   Life Totals In Visual Basic
  A friend of mine came up to me today and asked if I would be willing to make a game for him, he had a solid idea, a well written overview of how the game is played and some art. It's a simple card game, I decided it could be done easily in Visual Basic and would be good practice since I have to do it in school and I now have to learn how to program in visual basic. I have an idea of how most of the game should work but I'm having an issue with the life totals. For now, I'm just working on a simple script for changing a label depending on what the variable for life is. Here's my code:Public Class Life_Test    ' Define Life Total as Integer    Public Shared P1_Health As Integer    Public Shared P2_Health As Integer    Private Sub Life_Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   ' Set Health Variables to 25   P1_Health = 25   P2_Health = 25   'Reset Health Total Label   P1Health.Text = P1_Health   P2Health.Text = P2_Health    End Sub    Private Sub P1_Atk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Atk.Click   ' Deal 1 Damage to Player 2   P2_Health = P2_Health - 1   'Reset Health Total Label   P1Health.Text = P1_Health   P2Health.Text = P2_Health    End Sub    Private Sub P2_Atk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Atk.Click   ' Deal 1 Damage to Player 1   P1_Health = P1_Health - 1   'Reset Health Total Label   P1Health.Text = P1_Health   P2Health.Text = P2_Health    End Sub    Private Sub P1_Heal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Atk.Click   ' Heal Player 1 for 1 life   P1_Health = P1_Health + 1   'Reset Health Total Label   P1Health.Text = P1_Health   P2Health.Text = P2_Health    End Sub    Private Sub P2_Heal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Atk.Click   ' Heal Player 2 for 1 life   P2_Health = P2_Health + 1   'Reset Health Total Label   P1Health.Text = P1_Health   P2Health.Text = P2_Health    End Sub    Private Sub Menu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu.Click   Me.Close()   Startup.Show()    End SubEnd ClassWhat it should be doing, is when you start the form, the variable gets set to 25 as an integer. When you click on the Player 1 attack button, player 2 loses a life, etc. Then the label shows the variable again which should be different, but it isn't changing at all.I think what is happening is the variable is getting changed, but when the label gets set to the variable it looks at the original declaration. How do I fix this?   
  Why is health declared as shared?Also, if you use properties, you can change the text in your labels when the value of the variable changes.Public Property Player1Health() As Integer  Get()    Return P1_Health  End Get  Set(ByVale value As Integer)    P1_Health = value    P1Health.Text = value.ToString()  End SetEnd PropertyDoing it this way, you only define the value behavior once, and it will be much easier for you to fix:Public Class Life_Test    ' Define Life Total as Integer    Private P1_Health As Integer    Private P2_Health As Integer    Public Property Player1Health() As Integer   Get()  Return P1_Health   End Get   Set(ByVale value As Integer)  P1_Health = value  P1Health.Text = value.ToString()   End Set    End Property    Public Property Player2Health() As Integer   Get()  Return P2_Health   End Get   Set(ByVale value As Integer)  P2_Health = value  P2Health.Text = value.ToString()   End Set    End Property    Private Sub Life_Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   ' Set Health Variables to 25   Player1Health = 25   Player2Health = 25    End Sub    Private Sub P1_Atk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Atk.Click   ' Deal 1 Damage to Player 2   Player2Health -= 1    End Sub    Private Sub P2_Atk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Atk.Click   ' Deal 1 Damage to Player 1   Player1Health -= 1    End Sub    Private Sub P1_Heal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Atk.Click   ' Heal Player 1 for 1 life   Player1Health += 1    End Sub    Private Sub P2_Heal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Atk.Click   ' Heal Player 2 for 1 life   Player2Health += 1    End Sub    Private Sub Menu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu.Click   Me.Close()   Startup.Show()    End SubEnd Class;  Thanks a lot for the response, I have health shared because when the actual game is running, other forms besides the main playing field form will use data from the match. I'm getting an error on the properties though, a syntax error on the get parenthesis. Any idea why?   ;  Is this the only form running when you are testing?Do the labels you are looking at definately live on this forM ?Are you closing this form and reppening it throughout he test (therefore red paring your values)   ;  Yes it's the only form running for this test, the labels are definitely there, the code is just    Public Property Player1Health() As Integer   Get()  Return P1_Health()   End Get   Set(ByVal value As Integer)  P1_Health = value  P1Health.Text = value.ToString()   End Set    End Property    Public Property Player2Health() As Integer   Get()  Return P2_Health   End Get   Set(ByVal value As Integer)  P2_Health = value  P2Health.Text = value.ToString()   End Set    End PropertyThe Get() lines say Syntax error and the Player1/2Health() say Property without a ReadOnly or WriteOnly specifier must provide both a 'Get' and a 'Set'No I'm not reopening the forms.   ;  VB.NET or VB6?   ;  VB.Net   ;  Sorry stupid question. You're using handles....Before you read below, PLEASE DO NOT COPY AND PASTE THE ANSWERS. Just edit your code so you understand where you went wrong and use the below examples for comparison, it will help you learn.1)Ok, you're problem is this, both of your heal procedures are called by the attack buttons. Change "Handles P1_Atk.Click" to "Handles P1_Heal.Click" for your healing procedures.    Private Sub P1_Heal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_Heal.Click   ' Heal Player 1 for 1 life   P1_Health = P1_Health + 1   'Reset Health Total Label   P1Health.Text = P1_Health   P2Health.Text = P2_Health    End Sub    Private Sub P2_Heal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P2_Heal.Click   ' Heal Player 2 for 1 life   P2_Health = P2_Health + 1   'Reset Health Total Label   P1Health.Text = P1_Health   P2Health.Text = P2_Health    End Sub2) You don't put () after Get in your Get/Set property code. Also, you only use () when you are calling a procedure or function so you also don't need the ()'s after the return value "Return P1_Health"    Public Property Player1Health() As Integer   Get  Return P1_Health   End Get   Set(ByVal value As Integer)  P1_Health = value  P1Health.Text = value.ToString()   End Set    End Property    Public Property Player2Health() As Integer   Get  Return P2_Health   End Get   Set(ByVal value As Integer)  P2_Health = value  P2Health.Text = value.ToString()   End Set    End Property   ;  I fixed the handles and it works I also got rid of the () which was actually put there automatically by Visual Basic I got rid of them and now it works. Thanks a lot for your help!   ;  No worries mate.Good luck with the game, when you are ready to move up to something a little more difficult check out Unity3D.   