Solved

Help using a loop to make code more efficient

Posted on 2006-06-15
8
226 Views
Last Modified: 2010-04-23
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
Comment
Question by:Hepen
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Expert Comment

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

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

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

by:
anyoneis earned 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

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

:-David
0
 
LVL 12

Expert Comment

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

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

Author Comment

by:Hepen
ID: 16917837
LOL nice
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

15 Experts available now in Live!

Get 1:1 Help Now