I'm starting to learn the basics of oop in school. I have a program that calculates a batting avg and the collection adds a number starting at 1, the name of the player and the batting avg. The problem is when I go to add another player the player gets added but the batting avg for the second player changes the avg for the first player to match the avg of the second player.
This is the code.....I also created a class for this program..I'll list the class after the main form code.
Public Class Form1
Inherits System.Windows.Forms.Form
Dim objBallPlayer As BallPlayer
Dim Stats As New Collection
Dim sPlayerNum As Integer
Dim sPlayerName As String
Dim intAtBats As Integer
Dim intHits As Integer
Dim intAvg As Double
Shadows name As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInfo.Click
name = CStr(txtName.Text)
intHits = CInt(txtHits.Text)
intAtBats = CInt(txtAtBats.Text)
objBallPlayer = New BallPlayer(sPlayerName)
intAvg = objBallPlayer.CalcBattingAvg(intHits, intAtBats)
txtAvg.Text = intAvg
If txtName.Text <> "" Then
sPlayerNum = Stats.Count + 1
txtPlayerNum.Enabled = True
txtPlayerNum.Text = CStr(sPlayerNum)
txtName.Enabled = True
objBallPlayer = New BallPlayer(sPlayerName)
txtName.Text = CStr(name)
txtHits.Enabled = True
txtAtBats.Enabled = True
objBallPlayer = New BallPlayer(sPlayerNum)
objBallPlayer.PlayerName = txtName.Text
objBallPlayer.Hits = txtHits.Text
objBallPlayer.AtBats = txtAtBats.Text
Stats.Add(objBallPlayer, CStr(objBallPlayer.PlayerNum))
txtName.Enabled = False
txtPlayerNum.Enabled = False
txtHits.Enabled = False
txtAtBats.Enabled = False
txtPlayerNum.Enabled = False
txtAvg.Enabled = False
btnAdd.Focus()
Else
txtName.Focus()
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
txtName.Enabled = True
txtName.Text = ""
txtHits.Enabled = True
txtHits.Text = ""
txtAtBats.Enabled = True
txtAtBats.Text = ""
txtPlayerNum.Enabled = True
txtPlayerNum.Text = ""
txtPlayerNum.Enabled = False
txtAvg.Enabled = True
txtAvg.Text = ""
txtAvg.Enabled = False
txtName.Focus()
End Sub
Private Sub btnAddPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPlayer.Click
Try
lstStats.Items.Clear()
For Each objBallPlayer In Stats
Dim playerStat As String
playerStat = CInt(objBallPlayer.PlayerNum) & " " & objBallPlayer.PlayerName & " " & "Batting Average " & " " & intAvg
lstStats.Items.Add(playerStat)
lstStats.Refresh()
Next objBallPlayer
btnAdd.Focus()
Catch
End Try
End Sub
Private Sub lstStats_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstStats.SelectedIndexChanged
End Sub
Private Sub txtPlayerNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPlayerNum.TextChanged
End Sub
End Class
Here's the class code.....
Public Class BallPlayer
Private m_playerName As String
Private m_atBats As Integer
Private m_hits As Integer
Private m_battingAvg As Double
Private mPlayerNum As String
Public Sub New(ByVal sPlayerNum As Integer)
mPlayerNum = sPlayerNum
End Sub
Public ReadOnly Property PlayerNum() As Integer
Get
Return mPlayerNum
End Get
' Set(ByVal value As Integer)
' mPlayerNum = value
' End Set
End Property
Public Property PlayerName() As String
Get
Return m_playerName
End Get
Set(ByVal Value As String)
m_playerName = Value
End Set
End Property
Public Property AtBats() As Integer
Get
Return m_atBats
End Get
Set(ByVal Value As Integer)
m_atBats = Value
End Set
End Property
Public Property Hits() As Integer
Get
Return m_hits
End Get
Set(ByVal Value As Integer)
m_hits = Value
End Set
End Property
Public Property Avg() As Integer
Get
Return m_battingAvg
End Get
Set(ByVal Value As Integer)
m_battingAvg = Value
End Set
End Property
Function CalcBattingAvg(ByVal m_hits As Integer, ByVal m_atBats As Integer) As Double
Dim m_battingAvg As Double
' m_battingAvg = CDbl(m_hits) / CDbl(m_atBats)
' m_battingAvg = Math.Round(m_battingAvg, 3)
m_battingAvg = Math.Round(Val(m_hits) / Val(m_atBats), 3)
Return m_battingAvg
End Function
End Class
Can someone please point me in the right direction....Thanks