?
Solved

Smarter way of looping through object property and assign value

Posted on 2014-09-15
6
Medium Priority
?
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 49

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 49

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

777 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