Solved

# Smarter way of looping through object property and assign value

Posted on 2014-09-15
152 Views
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
``````

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?
0
Question by:miketonny
• 2
• 2

LVL 45

Expert Comment

ID: 40324429
If you are just going to have 5 zones like the 2 you show, that's not "massive". What am I missing?
0

LVL 2

Author Comment

ID: 40324447
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

``````

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

``````

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?
0

LVL 45

Expert Comment

ID: 40324502
Sorry but I don't think I have anything useful to contribute.
0

LVL 15

Accepted Solution

ID: 40324632
Why not create a Zones collection in Zone

Private _Zones as New Collection
Public Read Only Property Zones as Collection
Get
Return _Zones
End get
End property

Then add a method in Zones

Public Sub AddZone(index as Integer, StartValue as Decimal, endValue as decimal)
End Sub

And Create a constructor of ZoneDetail to accept the two parameters and set the zoneDetails properties.
0

LVL 2

Author Closing Comment

ID: 40329559
thanks
0

## Featured Post

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…