Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Help using a loop to make code more efficient

Posted on 2006-06-15
Medium Priority
267 Views
Is it possible to make the following code more efficient using a loop? I tried but can't figure it out.

Dim LocationX as integer
Dim LocationY as integer

'nw
Dim shrtNWCoordinateX As Short = LocationX + 16
Dim shrtNWCoordinateY As Short = LocationY - 16
Dim shrtNWZone As Short = GetZoneID(shrtNWCoordinateX, shrtNWCoordinateY)

'ne
Dim shrtNECoordinateX As Short = LocationX + 16
Dim shrtNECoordinateY As Short = LocationY + 16
Dim shrtNEZone As Short = GetZoneID(shrtNECoordinateX, shrtNECoordinateY)

'sw
Dim shrtSWCoordinateX As Short = LocationX - 16
Dim shrtSWCoordinateY As Short = LocationY - 16
Dim shrtSWZone As Short = GetZoneID(shrtSWCoordinateX, shrtSWCoordinateY)

'se
Dim shrtSECoordinateX As Short = LocationX - 16
Dim shrtSECoordinateY As Short = LocationY + 16
Dim shrtSEZone As Short = GetZoneID(shrtSECoordinateX, shrtSECoordinateY)
0
Question by:Hepen
[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
• 4
• 2
• 2

LVL 11

Expert Comment

ID: 16917653
What do you mean by more efficient? Faster executing? Less typing?

Also, are all 3 of the results needed or do you just need the zone?

David
0

Author Comment

ID: 16917662
all the results are needed because the zones might be different

more efficient meaning less lines of code but maybe with a loop . it seems i'm storing four different zone values in four different variables? I heard of arrays or something 'm not sure.
0

LVL 11

Expert Comment

ID: 16917673
There's a few less additions here:

'nw
Dim shrtNWCoordinateX As Short = LocationX + 16
Dim shrtNWCoordinateY As Short = LocationY - 16
Dim shrtNWZone As Short = GetZoneID(shrtNWCoordinateX, shrtNWCoordinateY)

'ne
Dim shrtNECoordinateX As Short = shrtNWCoordinateX
Dim shrtNECoordinateY As Short = LocationY + 16
Dim shrtNEZone As Short = GetZoneID(shrtNECoordinateX, shrtNECoordinateY)

'sw
Dim shrtSWCoordinateX As Short = LocationX - 16
Dim shrtSWCoordinateY As Short = shrtNWCoordinateY
Dim shrtSWZone As Short = GetZoneID(shrtSWCoordinateX, shrtSWCoordinateY)

'se
Dim shrtSECoordinateX As Short = shrtSWCoordinateX
Dim shrtSECoordinateY As Short = shrtNECoordinateY
Dim shrtSEZone As Short = GetZoneID(shrtSECoordinateX, shrtSECoordinateY)
0

LVL 11

Accepted Solution

anyoneis earned 2000 total points
ID: 16917696
By needing all results, I mean't something like this

'nw
Dim shrtNWZone As Short = GetZoneID(LocationX + 16, LocationY - 16)

'ne
Dim shrtNEZone As Short = GetZoneID(LocationX + 16, LocationY + 16)

'sw
Dim shrtSWZone As Short = GetZoneID(LocationX - 16, LocationY - 16)

'se
Dim shrtSEZone As Short = GetZoneID(LocationX - 16, LocationY + 16)

0

LVL 11

Expert Comment

ID: 16917698
To answer your question directly, I don't see how loops would help here.

:-David
0

LVL 12

Expert Comment

ID: 16917817
Not that you need it, but I had some fun with a loop and sin and cos: :-)

Dim LocationX As Integer
Dim LocationY As Integer

Dim NewLocationX As Integer
Dim NewLocationY As Integer
Dim i As Integer
Dim r As Single
Dim k As Single
k = (Math.Sqrt(2) / 2)

Dim GoDistance As Integer = 16

For i = 0 To 3

r = (i * 2 + 1) / 4

NewLocationX = Math.Sin(Math.PI * r) / k * GoDistance + LocationX
NewLocationY = Math.Cos(Math.PI * r) / k * GoDistance + LocationY

Select Case i
Case 0 'nw
Dim shrtNWZone As Short = GetZoneId(NewLocationX, NewLocationY)
Case 1 'sw
Dim shrtSWZone As Short = GetZoneId(NewLocationX, NewLocationY)
Case 2 'se
Dim shrtSEZone As Short = GetZoneId(NewLocationX, NewLocationY)
Case 3 'ne
Dim shrtNeZone As Short = GetZoneId(NewLocationX, NewLocationY)
End Select

Next
0

LVL 12

Expert Comment

ID: 16917835
sorry swap the nw and ne, sw and se. case 0 'ne ...
0

Author Comment

ID: 16917837
LOL nice
0

## Featured Post

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
###### Suggested Courses
Course of the Month4 days, 8 hours left to enroll