Solved

Smarter way of looping through object property and assign value

Posted on 2014-09-15
6
152 Views
Last Modified: 2014-09-17
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?
0
Comment
Question by:miketonny
  • 2
  • 2
6 Comments
 
LVL 45

Expert Comment

by:Martin Liss
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

by:miketonny
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
	

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

Expert Comment

by:Martin Liss
ID: 40324502
Sorry but I don't think I have anything useful to contribute.
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
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)
 Zones.Add(index,Newe ZoneDetail(startValue,EndValue)
End Sub

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

Author Closing Comment

by:miketonny
ID: 40329559
thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now