Passing class info by value rather than by reference

jbrookley

Member
Joined
Jan 8, 2009
Messages
11
Programming Experience
1-3
Hey guys,

This is probably an easy fix but at this point, I don't know it . . . heh.

VB.NET:
Public Class Form1

Public Contestant As New ContestantClass
    Public HighScore1 As New ContestantClass
    Public HighScore2 As New ContestantClass
    Public HighScore3 As New ContestantClass
    Public HighScore4 As New ContestantClass
    Public HighScore5 As New ContestantClass

   Public Class ContestantClass

        Private tempname As String
        Private tempcompany As String
        Private tempscore As String

        Public Property Name() As String
            Get
                Return tempname
            End Get
            Set(ByVal Value As String)
                tempname = Value
            End Set
        End Property

        Public Property Company() As String
            Get
                Return tempcompany
            End Get
            Set(ByVal Value As String)
                tempcompany = Value
            End Set
        End Property

        Public Property Score() As Decimal
            Get
                Return tempscore
            End Get
            Set(ByVal Value As Decimal)
                tempscore = Value
            End Set
        End Property

    End Class

   Function InsertScore()

        If Contestant.Score > HighScore1.Score Then
            HighScore5 = HighScore4
            HighScore4 = HighScore3
            HighScore3 = HighScore2
            HighScore2 = HighScore1
            HighScore1 = Contestant
        ElseIf Contestant.Score > HighScore2.Score Then
            HighScore5 = HighScore4
            HighScore4 = HighScore3
            HighScore3 = HighScore2
            HighScore2 = Contestant
        ElseIf Contestant.Score > HighScore3.Score Then
            HighScore5 = HighScore4
            HighScore4 = HighScore3
            HighScore3 = Contestant
        ElseIf Contestant.Score > HighScore4.Score Then
            HighScore5 = HighScore4
            HighScore4 = Contestant
        ElseIf Contestant.Score > HighScore5.Score Then
            HighScore5 = Contestant
        End If

        UpdateHighScore()

        Return 0
    End Function

   Function UpdateHighScore()

        HighScoreBox.Items.Clear()
        HighScoreBox.Items.Add(HighScore1.Name & vbTab & HighScore1.Company & vbTab & HighScore1.Score)
        HighScoreBox.Items.Add(HighScore2.Name & vbTab & HighScore2.Company & vbTab & HighScore2.Score)
        HighScoreBox.Items.Add(HighScore3.Name & vbTab & HighScore3.Company & vbTab & HighScore3.Score)
        HighScoreBox.Items.Add(HighScore4.Name & vbTab & HighScore4.Company & vbTab & HighScore4.Score)
        HighScoreBox.Items.Add(HighScore5.Name & vbTab & HighScore5.Company & vbTab & HighScore5.Score)

        Return 0
    End Function
End Class

The problem I'm having is the first time I take a value, it gets inserted in the right section. The second time I insert a value, it inserts it in the correct place AND updates the previous value I inserted with the new value. So apparently when I'm doing:

VB.NET:
HighScore1 = Contestant

it's pointing contestant AT highscore1 rather than taking the name, company, and score from contestant and then storing it in highscore1.

Anyone know how I can pass that info by value rather than by reference?

Thanks!
 
That's how reference types work. If you want to transfer the property values from one object to another then that's exactly what you need to do: transfer the property values. You'll need to either assign each property individually or else you can make your class implement the IClonable interface and implment a Clone method. You can then use the MemberwiseClone method internally rather than have to copy each property individually.
 
Sounds good. I ended up adding a Clone() function to my class so it looks like this:

VB.NET:
    Public Class ContestantClass

        Private tempname As String
        Private tempcompany As String
        Private tempscore As String

        Public Property Name() As String
            Get
                Return tempname
            End Get
            Set(ByVal Value As String)
                tempname = Value
            End Set
        End Property

        Public Property Company() As String
            Get
                Return tempcompany
            End Get
            Set(ByVal Value As String)
                tempcompany = Value
            End Set
        End Property

        Public Property Score() As Decimal
            Get
                Return tempscore
            End Get
            Set(ByVal Value As Decimal)
                tempscore = Value
            End Set
        End Property

        Public Function Clone() As ContestantClass
            Return CType(Me.MemberwiseClone, ContestantClass)
        End Function

    End Class

    Function InsertScore()

        If Contestant.Score > HighScore1.Score Then
            HighScore5 = HighScore4.Clone
            HighScore4 = HighScore3.Clone
            HighScore3 = HighScore2.Clone
            HighScore2 = HighScore1.Clone
            HighScore1 = Contestant.Clone
        ElseIf Contestant.Score > HighScore2.Score Then
            HighScore5 = HighScore4.Clone
            HighScore4 = HighScore3.Clone
            HighScore3 = HighScore2.Clone
            HighScore2 = Contestant.Clone
        ElseIf Contestant.Score > HighScore3.Score Then
            HighScore5 = HighScore4.Clone
            HighScore4 = HighScore3.Clone
            HighScore3 = Contestant.Clone
        ElseIf Contestant.Score > HighScore4.Score Then
            HighScore5 = HighScore4.Clone
            HighScore4 = Contestant.Clone
        ElseIf Contestant.Score > HighScore5.Score Then
            HighScore5 = Contestant.Clone
        End If

        UpdateHighScore()

        Return 0
    End Function

Thanks for your help!
 
Back
Top