Link to home
Start Free TrialLog in
Avatar of miketonny
miketonny

asked on

Smarter way of looping through object property and assign value

Hi ,
   I've got below code written

  Public Function FillZoneDetails() As Zones
                Dim z As New Zones()
                With z
                    With .Zone1
                        .ZoneStartPoint = 0
                        .ZoneEndPoint = _Settings.MaxVelocity * (_Settings.SpeedZone1 / 100) * 1
                    End With
                    With Zone2
                        .ZoneStartPoint = Zone1.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxVelocity * (_Settings.SpeedZone2 / 100) * 1
                    End With
                    .......   'up until zone5

                End With
                  
     End Function

Open in new window


 basically, I'll have a list of different zones and their details will be calculated based on different factor, but I can tell by the stuff I have this function is going to be massive once all zones are filled.
I'm wondering, is there a smarter way of constructing this object? or shall I change the structure of the zone class somehow?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

If you are just going to have 5 zones like the 2 you show, that's not "massive". What am I missing?
Avatar of miketonny
miketonny

ASKER

I will have zones based on (speed,accelaration, decclearation,heartrate, etc)

to instantiate all the zones, i'll need to write this function something like:
   Public Function FillZoneDetails(ByVal zoneDef As ZoneDef) As Zones
        Select Case zoneDef
            Case Zones.ZoneDef.Speed
                Dim z As New Zones(Zones.ZoneDef.Speed)
                With z
                    With .Zone1
                        .ZoneStartPoint = 0
                        .ZoneEndPoint = _Settings.MaxVelocity * (_Settings.SpeedZone1 / 100) * 1
                    End With
                    With Zone2
                        .ZoneStartPoint = Zone1.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxVelocity * (_Settings.SpeedZone2 / 100) * 1
                    End With
                    With Zone3
                        .ZoneStartPoint = Zone2.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxVelocity * (_Settings.SpeedZone3 / 100) * 1
                    End With
                    With Zone4
                        .ZoneStartPoint = Zone3.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxVelocity * (_Settings.SpeedZone4 / 100) * 1
                    End With
                    With Zone5
                        .ZoneStartPoint = Zone4.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxVelocity * 1
                    End With
                End With
            Case Zones.ZoneDef.Accelaration
                Dim z As New Zones(Zones.ZoneDef.Accelaration)
                With z
                    With .Zone1
                        .ZoneStartPoint = 0
                        .ZoneEndPoint = _Settings.MaxAccelaration * (_Settings.AccelZone1 / 100) * 1
                    End With
                    With Zone2
                        .ZoneStartPoint = Zone1.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxAccelaration * (_Settings.AccelZone2 / 100) * 1
                    End With
                    With Zone3
                        .ZoneStartPoint = Zone2.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxAccelaration * (_Settings.AccelZone3 / 100) * 1
                    End With
                    With Zone4
                        .ZoneStartPoint = Zone3.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxAccelaration * (_Settings.AccelZone4 / 100) * 1
                    End With
                    With Zone5
                        .ZoneStartPoint = Zone4.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxAccelaration * 1
                    End With
                End With
            Case Zones.ZoneDef.HR
                Dim z As New Zones(Zones.ZoneDef.HR)
                With z
                    With .Zone1
                        .ZoneStartPoint = 0
                        .ZoneEndPoint = _Settings.MaxHR * (_Settings.HRZone1.Y / 100) * 1
                    End With
                    With Zone2
                        .ZoneStartPoint = Zone1.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxHR * (_Settings.HRZone2.Y / 100) * 1
                    End With
                    With Zone3
                        .ZoneStartPoint = Zone2.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxHR * (_Settings.HRZone3.Y / 100) * 1
                    End With
                    With Zone4
                        .ZoneStartPoint = Zone3.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxHR * (_Settings.HRZone4.Y / 100) * 1
                    End With
                    With Zone5
                        .ZoneStartPoint = Zone4.ZoneEndPoint
                        .ZoneEndPoint = _Settings.MaxHR * 1
                    End With
                End With


        End Select




    End Function
	

Open in new window



current  property of zones class

    Public Property Zone1 As ZoneDetail
        Get
            Return _zone1
        End Get
        Set(ByVal value As ZoneDetail)
            _zone1 = value
        End Set
    End Property
    Private _zone1 As ZoneDetail

    Public Property Zone2 As ZoneDetail
        Get
            Return _zone2
        End Get
        Set(ByVal value As ZoneDetail)
            _zone2 = value
        End Set
    End Property
    Private _zone2 As ZoneDetail

    Public Property Zone3 As ZoneDetail
        Get
            Return _zone3
        End Get
        Set(ByVal value As ZoneDetail)
            _zone3 = value
        End Set
    End Property
    Private _zone3 As ZoneDetail

    Public Property Zone4 As ZoneDetail
        Get
            Return _zone4
        End Get
        Set(ByVal value As ZoneDetail)
            _zone4 = value
        End Set
    End Property
    Private _zone4 As ZoneDetail

    Public Property Zone5 As ZoneDetail
        Get
            Return _zone5
        End Get
        Set(ByVal value As ZoneDetail)
            _zone5 = value
        End Set
    End Property
    Private _zone5 As ZoneDetail



    Public Class ZoneDetail
        Public Property ZoneStartPoint As Decimal
            Get
                Return _startPt
            End Get
            Set(ByVal value As Decimal)
                _startPt = value
            End Set
        End Property
        Private _startPt As Decimal
        Public Property ZoneEndPoint As Decimal
            Get
                Return _endPt
            End Get
            Set(ByVal value As Decimal)
                _endPt = value
            End Set
        End Property
        Private _endPt As Decimal

    End Class
	

Open in new window


these just to start with and i can see a lot of repetitive codes already, I'm planning to expand the zones to 10+ later on.

the common thing between zone1-zone5 properties are their 'StartPoint' is always going to be the 'EndPoint'  of previous zone, is there something I could do to improve this part?
Sorry but I don't think I have anything useful to contribute.
ASKER CERTIFIED SOLUTION
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks