none
Ammo Variable RRS feed

  • Question

  •     Dim Ammo As Integer = 30
    
        Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Ammo = Ammo - 1
            Lbl1_Ammo.Text = Ammo
        End Sub
        Private Sub Btn2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Ammo = 30
            Lbl1_Ammo.Text = Ammo
        End Sub
    End Class

    I am practicing a gun simulator which could help in my coursework. 

    I am able to get the interaction to shoot and reload but I am not able to carry out the functions to put on and off the safety or put the gun into safety mode. 

    I can help to say that declaring Safety and Burst as Boolean as a start and it goes under an If statement. 

    Sunday, March 5, 2017 2:10 PM

Answers

  • In terms of this do I just say Inherits and the form I want the current form to inherit from then it works. plus gotta make sure when its in burst mode change the -1 to subtract maybe by 3 from the total ammo stored

    No you dont use inherit for this. That is used inside the class like say you want to add to a button then you can make a new class of your own that inherits button.;

    For this you make a class module in the project. Just like making a new form, but instead of selecting form you select class from the menu.

    Here is the class module:

    Public Class Gun
        Public ammo As Integer
        Public safety As Boolean
    
    End Class

    Here is the form:

    Option Strict On
    
    Public Class Form3
        Private Gun1 As New Gun
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Gun1.ammo = 30
            Button1.Text = "Fire Gun 1"
            RadioButton1.Text = "Safety On"
            RadioButton2.Text = "Safety Off"
            RadioButton1.Checked = True
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            GunFire(Gun1)
        End Sub
    
        Private Sub GunFire(thisGun As Gun)
            If Not thisGun.safety Then
                If thisGun.ammo > 0 Then
                    MsgBox("boom")
                    thisGun.ammo -= 1
                Else
                    MsgBox("Out of Ammo")
                End If
            Else
                MsgBox("Safety is ON")
            End If
        End Sub
    
        Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles _
            RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
    
            Gun1.safety = RadioButton1.Checked
        End Sub
    End Class

    Then in the solution explorer you see the form3 and the class gun.


    Sunday, March 5, 2017 4:02 PM
  • Maybe this will help. This example I made a button for the form in code. Not in the designer. The form is blank in the designer.

    See how I add the button by declaring it? Just like the gun class more or less.  Dim button as classname. Thats it (leaving out some details). Since our form is referencing windows forms class and button is part of that class we get to use the button without (referencing) it again. Windows form has already inherited the button.

    Public Class Form4
        'this adds the button control to the form
        'button class is referenced in the windows forms project
        Private WithEvents Button1 As New Button With {.Parent = Me, .Location = New Point(20, 20)}
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MsgBox("Button1 clicked")
        End Sub
    
    End Class

    See how in the image the windows forms is referenced for the project.


    See how the docs tell us what is what:

    But this is getting beyond what you need for just saving data variables in a class.


    PS Notice how it says at the top Public Class Form4? A form is a class. There are lots of kinds of classes and things that can be done with them. But this is all you need for now. And I am no expert at the classes. I just use them as I know how and can and sometimes I learn new things about them in the process.

    Sunday, March 5, 2017 4:28 PM

All replies

  • Sounds like a job for a simple class structure. Have you started using classes? If not you can use a structure too.


    Option Strict On
    
    Public Class Form3
        Private Class Gun
            Public ammo As Integer
            Public safety As Boolean
        End Class
        Private Gun1 As New Gun
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Gun1.ammo = 30
            Button1.Text = "Fire Gun 1"
            RadioButton1.Text = "Safety On"
            RadioButton2.Text = "Safety Off"
            RadioButton1.Checked = True
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            GunFire(Gun1)
        End Sub
    
        Private Sub GunFire(thisGun As Gun)
            If Not thisGun.safety Then
                If thisgun.ammo > 0 Then
                    MsgBox("boom")
                    thisGun.ammo -= 1
                Else
                    MsgBox("Out of Ammo")
                End If
            Else
                MsgBox("Safety is ON")
            End If
        End Sub
    
        Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles _
            RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
    
            Gun1.safety = RadioButton1.Checked
        End Sub
    End Class

    Edit 2: added handler for RadioButton2.CheckChanged

    PS Its just an example. Not perfect.

    PSS I made the example so its easy to recreate by cut and paste from the forum. There may be better ways ie to name things... But for now all that is important is to learn how the class is used to save the data in a variable(s). And then use the data in the controls.


    Sunday, March 5, 2017 2:26 PM
  • I have used classes in past experiences but that was an only one time thing and that was with a console application sadly. Ill take a shot with the one you have provided and msg back if there is an issue. Plus if Im referring a class how would I apply it to another form using "inheritance"?
    Sunday, March 5, 2017 2:30 PM
  • Could you possibly provide me with instruction when applying this code like do I need to create another form do I need a forced inherit function etc. It really helps me 
    Sunday, March 5, 2017 2:41 PM
  • I have used classes in past experiences but that was an only one time thing and that was with a console application sadly. Ill take a shot with the one you have provided and msg back if there is an issue. Plus if Im referring a class how would I apply it to another form using "inheritance"?

    I updated the example to add some radiobuttons for the safety.

    You can add the class to your project and declare it like I did in the example more or less. However I made the class private to the form. So to use it on many forms you can make a class module and add the class module to your project. The ref the class from your form.

    But one step at a time...

    Read the instructions. Learn what I have shown first. Then add to it.


    PS there are 100's of ways to do this. This is just a start. I am sure others will have more to say.
    Sunday, March 5, 2017 2:41 PM
  • In terms of this do I just say Inherits and the form I want the current form to inherit from then it works. plus gotta make sure when its in burst mode change the -1 to subtract maybe by 3 from the total ammo stored
    Sunday, March 5, 2017 3:28 PM
  • In terms of this do I just say Inherits and the form I want the current form to inherit from then it works. plus gotta make sure when its in burst mode change the -1 to subtract maybe by 3 from the total ammo stored

    No you dont use inherit for this. That is used inside the class like say you want to add to a button then you can make a new class of your own that inherits button.;

    For this you make a class module in the project. Just like making a new form, but instead of selecting form you select class from the menu.

    Here is the class module:

    Public Class Gun
        Public ammo As Integer
        Public safety As Boolean
    
    End Class

    Here is the form:

    Option Strict On
    
    Public Class Form3
        Private Gun1 As New Gun
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Gun1.ammo = 30
            Button1.Text = "Fire Gun 1"
            RadioButton1.Text = "Safety On"
            RadioButton2.Text = "Safety Off"
            RadioButton1.Checked = True
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            GunFire(Gun1)
        End Sub
    
        Private Sub GunFire(thisGun As Gun)
            If Not thisGun.safety Then
                If thisGun.ammo > 0 Then
                    MsgBox("boom")
                    thisGun.ammo -= 1
                Else
                    MsgBox("Out of Ammo")
                End If
            Else
                MsgBox("Safety is ON")
            End If
        End Sub
    
        Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles _
            RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
    
            Gun1.safety = RadioButton1.Checked
        End Sub
    End Class

    Then in the solution explorer you see the form3 and the class gun.


    Sunday, March 5, 2017 4:02 PM
  • Maybe this will help. This example I made a button for the form in code. Not in the designer. The form is blank in the designer.

    See how I add the button by declaring it? Just like the gun class more or less.  Dim button as classname. Thats it (leaving out some details). Since our form is referencing windows forms class and button is part of that class we get to use the button without (referencing) it again. Windows form has already inherited the button.

    Public Class Form4
        'this adds the button control to the form
        'button class is referenced in the windows forms project
        Private WithEvents Button1 As New Button With {.Parent = Me, .Location = New Point(20, 20)}
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MsgBox("Button1 clicked")
        End Sub
    
    End Class

    See how in the image the windows forms is referenced for the project.


    See how the docs tell us what is what:

    But this is getting beyond what you need for just saving data variables in a class.


    PS Notice how it says at the top Public Class Form4? A form is a class. There are lots of kinds of classes and things that can be done with them. But this is all you need for now. And I am no expert at the classes. I just use them as I know how and can and sometimes I learn new things about them in the process.

    Sunday, March 5, 2017 4:28 PM