Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating a list(t)

Status
Not open for further replies.

omacron

Technical User
Feb 5, 2002
149
I have a list(t) defined as;
Code:
Public Class typeList
    Private mType1 As String
    Private mType2 As String
    Private mType3 As String
    Private mType4 As String
    Private mTotal As Double

    
    Public Property Total() As Double
        Get
            Return mTotal
        End Get
        Set(ByVal value As Double)
            mTotal = value
        End Set
    End Property

    Public Property Type4() As String
        Get
            Return mType4
        End Get
        Set(ByVal value As String)
            mType4 = value
        End Set
    End Property
    Public Property Type3() As String
        Get
            Return mType3
        End Get
        Set(ByVal value As String)
            mType3 = value
        End Set
    End Property
    Public Property Type2() As String
        Get
            Return mType2
        End Get
        Set(ByVal value As String)
            mType2 = value
        End Set
    End Property
    Public Property Type1() As String
        Get
            Return mType1
        End Get
        Set(ByVal value As String)
            mType1 = value
        End Set
    End Property
    Public Sub New()

    End Sub
End Class
Now I need to update the Total based on conditions of Type1 through Type4. The way I am currently doing this is with a for each loop;
Code:
For Each ob In typeList
	If iType1 = ob.Type1 And iType2 = ob.Type2 And iType3 = ob.Type3 And iType4 = ob.Type4 Then
		ob.Total = iTotal
	End If
Next
This works fine but there has to be a better way to do this.

Thanks for your help
 
Since they are properties fire an event each time one is changed to update the total.

Code:
Public Class typeList
    Private mType1 As Double
    Private mType2 As Double
    Private mType3 As Double
    Private mType4 As Double
    Private mTotal As Double


    Public Property Total() As Double
        Get
            Return mTotal
        End Get
        Set(ByVal value As Double)
            mTotal = value
        End Set
    End Property

    Public Property Type4() As Double
        Get
            Return mType4
        End Get
        Set(ByVal value As Double)
            mType4 = value

            RaiseEvent OnTypeAdd(value)
        End Set
    End Property
    Public Property Type3() As Double
        Get
            Return mType3
        End Get
        Set(ByVal value As Double)
            mType3 = value

            RaiseEvent OnTypeAdd(value)
        End Set
    End Property
    Public Property Type2() As Double
        Get
            Return mType2
        End Get
        Set(ByVal value As Double)
            mType2 = value

            RaiseEvent OnTypeAdd(value)
        End Set
    End Property
    Public Property Type1() As Double
        Get
            Return mType1
        End Get
        Set(ByVal value As Double)
            mType1 = value

            RaiseEvent OnTypeAdd(value)
        End Set
    End Property

    Public Sub New()

    End Sub

    Private Event OnTypeAdd(ByVal value As Double)
    Private Sub TypeAdd(ByVal value As Double) Handles Me.OnTypeAdd
        Total += value
    End Sub

End Class

Doing it as an event means it has more growth potential. Also note I changed everything to double. You should always start as you are going to go. Don't open your class up to ending up with 4 + 3 + Cat because you missed something as your program grows.


-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Oops! Add to mTotal not Total(mTotal += value).

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I also forgot to mention it beggs the question do you want people(or yourself if it is just you) actually setting Total. Think about making it ReadOnly.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top