borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Converting numbers to Words)Next Topic (import fields from XML file to ACCESS) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Milage calculator Solved Topic
Poster Message
mrcoolcurrent
Level: Big Cheese

Registered: 19-01-2007
Posts: 23

icon Milage calculator

Pls. I need help .
I want to make milage calculator to compute the distances between all the EU Nations.

I have the list of the countries and the distances.

I need an idea pls.

10-12-2007 at 08:49 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 922
icon Re: Milage calculator

Hello. What's changed since this other post of some months ago?

____________________________
Real Programmer can count up to 1024 on his fingers

10-12-2007 at 11:40 PM
View Profile Send Email to User Show All Posts | Quote Reply
mrcoolcurrent
Level: Big Cheese

Registered: 19-01-2007
Posts: 23
icon Re: Milage calculator

Yes there is lot of improvement!.
I have discovered that I have to use arrays.

I want to know the dis tance from every single location or city in this chart below to another.

http://www.parsons103.freeserve.co.uk/mileage.htm

Do I use If then for every city or what?

14-12-2007 at 06:31 PM
View Profile Send Email to User Show All Posts | Quote Reply
TheGoat
Level: Protégé

Registered: 13-12-2007
Posts: 4
icon Re: Milage calculator

The simplest way would be to use a 2 dimensional array. Create your input file, thus:

Boulogne, Caen, Calais, Cherbourg, Dieppe, Le Havre,  Roscoff, Rotterdam, St Malo, Zeebrugge
Alençon, 323, 102, 357, 222, 206, 174, 342, 589, 178, 453
Amiens, 122, 238, 154, 357, 114, 178, 595, 336, 416, 200
.
etc
.
Valence, 803, 786, 846, 904, 717, 749, 978, 902, 827, 830

Then, I'd use a couple of ListBoxes to hold the "From" towns and "To" towns and use the ListIndex properties to pick the correct element out of the array when the user selects two towns.

It's probably easier to show you some code as an example rather than try to describe the logic:

Option Explicit
Dim intKm(25, 9) As Integer
Dim intFrom As Integer

Private Sub Form_Load()
Dim intFile  As Integer
Dim intI As Integer
Dim intCount As Integer
Dim strLine As String
Dim strTemp() As String
'
' Initialise Controls and allocate a File Number
'
lstFrom.Clear
lstTo.Clear
txtDistance.Text = ""
intFile = FreeFile
'
' Open the data file and read the first record ("To" Towns)
'
Open "C:\Distance.csv" For Input As #intFile
Line Input #intFile, strLine
strTemp = Split(strLine, ",")
'
' Populate the "To:" ListBox
'
For intI = 0 To UBound(strTemp)
    lstTo.AddItem strTemp(intI)
Next intI
intCount = 0
'
' Read the rest of the data, populate the "From:" ListBox
' and populate the Distances array
'
Do
    Line Input #intFile, strLine
    strTemp = Split(strLine, ",")
    lstFrom.AddItem strTemp(0)
    For intI = 1 To UBound(strTemp)
        intKm(intCount, intI - 1) = CInt(strTemp(intI))
    Next intI
    intCount = intCount + 1
Loop Until EOF(intFile)
Close #intFile

End Sub

Private Sub lstFrom_Click()
'
' Save the index of the Town the user clicked on
'
intFrom = lstFrom.ListIndex
End Sub

Private Sub lstTo_Click()
'
' Populate the Distance textBox
'
txtDistance.Text = CStr(intKm(intFrom, lstTo.ListIndex))
End Sub


Click on an Item in the "From" ListBox and then on an item in the "To" ListBox and the distance between the two will be displayed in the textBox.

It's not tested and is very basic but it should give you a reasonable start.

[Edited by TheGoat on 15-12-2007 at 08:52 AM GMT]

15-12-2007 at 08:26 AM
View Profile Send Email to User Show All Posts | Quote Reply
mrcoolcurrent
Level: Big Cheese

Registered: 19-01-2007
Posts: 23
icon Re: Milage calculator

Thanks a million times. I am so happy.
But I tried something which I know is gonna be funny to advanced coders but I need you help on this one too.

The problem is I have 31 destinations and would like to get distance from anywhere on my list of 31 cities.

In my logic I have to write the same code 31*31 which is 961.
I need a better way to do it with less code.

Check out my project & the codes

Dim DISTANCE As Integer



'Computation for  distances TO location no 1 , ABEOKUTA


Private Sub cmdCalculate_Click()
  
  If cboFrom.ListIndex = 0 And cboTo.ListIndex = 0 Then



    MsgBox "CHANGE YOUR SELECTION SOURCE AND DESTINATION CANT BE THE SAME", vbCritical + vbOKOnly
    

   End If
    
            
                                    
If cboFrom.ListIndex = 0 And cboTo.ListIndex = 1 Then
  
  DISTANCE = 740
  
  lblDistance.Caption = DISTANCE & " KM" & " KM"
  
  ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 2 Then
  DISTANCE = 277
  
  lblDistance.Caption = DISTANCE & " KM" & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 3 Then
  DISTANCE = 468
  
  lblDistance.Caption = DISTANCE & " KM" & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 4 Then
  DISTANCE = 444
  
  lblDistance.Caption = DISTANCE & " KM" & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 5 Then
  DISTANCE = 1072
  
  lblDistance.Caption = DISTANCE & " KM" & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 6 Then
  DISTANCE = 329
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 7 Then
  DISTANCE = 756
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 8 Then
  DISTANCE = 765
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 9 Then
  DISTANCE = 1384
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 10 Then
  DISTANCE = 1140
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 11 Then
  DISTANCE = 577
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 12 Then
  DISTANCE = 77
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 13 Then
  DISTANCE = 81
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 14 Then
  DISTANCE = 236
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 15 Then
  DISTANCE = 1312
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 16 Then
  DISTANCE = 940
  
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 17 Then
  DISTANCE = 836
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 18 Then
  DISTANCE = 1086
  
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 19 Then
  DISTANCE = 1129
  
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 20 Then
  DISTANCE = 469
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 21 Then
  DISTANCE = 1536
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 22 Then
  DISTANCE = 821
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 23 Then
  DISTANCE = 673
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 24 Then
  DISTANCE = 167
  
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 25 Then
  DISTANCE = 565
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 26 Then
  DISTANCE = 693
  
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 27 Then
  DISTANCE = 969
  
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 28 Then
  DISTANCE = 634
  
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 29 Then
  DISTANCE = 715
  
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 0 And cboTo.ListIndex = 30 Then
  DISTANCE = 1428
  
  lblDistance.Caption = DISTANCE & " KM"
  
  'Computation for  distances TO location no 2, ABUJA
  
  ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 0 Then
  DISTANCE = 740
    
  lblDistance.Caption = DISTANCE & " KM"
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 2 Then
  DISTANCE = 700
    
  lblDistance.Caption = DISTANCE & " KM"
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 3 Then
  DISTANCE = 404
    
  lblDistance.Caption = DISTANCE & " KM"
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 4 Then
  DISTANCE = 440
    
  lblDistance.Caption = DISTANCE & " KM"
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 5 Then
  DISTANCE = 445
    
  lblDistance.Caption = DISTANCE & " KM"
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 6 Then
  DISTANCE = 450
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 7 Then
  DISTANCE = 573
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 8 Then
  DISTANCE = 857
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 9 Then
  DISTANCE = 757
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 10 Then
  DISTANCE = 512
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 11 Then
  DISTANCE = 595
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 12 Then
  DISTANCE = 659
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 13 Then
  DISTANCE = 879
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 14 Then
  DISTANCE = 500
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 15 Then
  DISTANCE = 691
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 16 Then
  DISTANCE = 313
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 17 Then
  DISTANCE = 180
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 18 Then
  DISTANCE = 442
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 19 Then
  DISTANCE = 533
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 20 Then
  DISTANCE = 138
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
  
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 21 Then
  DISTANCE = 908
    
  lblDistance.Caption = DISTANCE & " KM"
  
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 22 Then
  DISTANCE = 323
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 23 Then
  DISTANCE = 117
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 24 Then
  DISTANCE = 428
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 25 Then
  DISTANCE = 733
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 26 Then
  DISTANCE = 830
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 27 Then
  DISTANCE = 793
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 28 Then
  DISTANCE = 498
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 29 Then
  DISTANCE = 828
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    ElseIf cboFrom.ListIndex = 1 And cboTo.ListIndex = 30 Then
  DISTANCE = 855
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
    
    
'Computation for  distances TO location no 3, AKURE
    
    ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 0 Then
  DISTANCE = 277
    
  lblDistance.Caption = DISTANCE & " KM"
    
    
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 1 Then
  DISTANCE = 700
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 3 Then
  DISTANCE = 310
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 4 Then
  DISTANCE = 320
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 5 Then
  DISTANCE = 835
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 6 Then
  DISTANCE = 171
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 7 Then
  DISTANCE = 711
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 8 Then
  DISTANCE = 607
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 9 Then
  DISTANCE = 1147
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 10 Then
  DISTANCE = 1070
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 11 Then
  DISTANCE = 389
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 12 Then
  DISTANCE = 200
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 13 Then
  DISTANCE = 312
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 14 Then
  DISTANCE = 191
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 15 Then
  DISTANCE = 880
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 16 Then
  DISTANCE = 703
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 17 Then
  DISTANCE = 761
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 18 Then
  DISTANCE = 1023
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 19 Then
  DISTANCE = 1084
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 20 Then
  DISTANCE = 232
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 21 Then
  DISTANCE = 1299
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 22 Then
  DISTANCE = 551
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 23 Then
  DISTANCE = 543
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 24 Then
  DISTANCE = 118
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 25 Then
  DISTANCE = 407
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 26 Then
  DISTANCE = 535
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 27 Then
  DISTANCE = 924
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 28 Then
  DISTANCE = 476
    
  lblDistance.Caption = DISTANCE & " KM"
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 29 Then
  DISTANCE = 557
    
  lblDistance.Caption = DISTANCE & " KM"
  
  
   ElseIf cboFrom.ListIndex = 2 And cboTo.ListIndex = 30 Then
  DISTANCE = 1166
    
  lblDistance.Caption = DISTANCE & " KM"
  
  End If
  
  
  
End Sub

15-12-2007 at 08:44 PM
View Profile Send Email to User Show All Posts | Quote Reply
TheGoat
Level: Protégé

Registered: 13-12-2007
Posts: 4
icon Re: Milage calculator

I think you may have missed the point of using Arrays to hold the data. Many people struggle with Arrays and then suddenly it all falls into place.  

If you look at your code, you are repeating the same logic over and over again, the only things that are changing are the ListIndexes and the Mileages.

If you were to think about those Mileage charts you often see in the back of MapBooks and Diaries, something like:

       A     B     C     D     E
A     0    10     20   30   40

B    10    0      15   20   30

C    20   15     0     25   30

D    30   20    25    0     40

E    40   30    30    40    0

So you can see that from A to E is 40 Miles, D to C is 25 Miles etc. If you look carefully you can also see it's symmetric about the diagonal represented by the "0" characters (Zero Miles), in the sense that B to C is exactly the same as C to B. You could use that symmetry to reduce the amount of data you have to input.

Also, if you think about the mechanism of determining the distance, using the Matrix above, it goes like this:

Find the "From" Place Row
Find the "To" Place Column
The Distance between them is the value at the intersection of the Row and Column

So, irrespective of the number of places, or the places themselves, the method to find the distance is always the same. All we have to do is to somehow represent the matrix of distances in our code, and then find a way to index into the Row and Column.

That's where Arrays come in. You can think of a 2 dimensional Array as looking exactly like the Matrix above

eg
Array(Row, Column)
So again, in the example above, we'd populate the array like this:

Array(0,0) = 0   -> First Row, First Column
Array(0,1) = 10 -> First Row, 2nd Column
Array(0,2) = 20 -> First Row, 3rd Column
Array(0,3) = 30 -> First Row, 4th Coumn
Array(0,4) = 40 -> First Row, 5th Column
Array(1,0) = 10 -> Second Row, First Column
and so on until we get to
Array(4,4) = 0 ->Fifth Row, 5th Column

Now, the clever bit. If you notice, all the distances from A to everywhere else are in Row 1, all the distances from B to everywhere else are in Row 2, and so on.  Also, all the distances from A to everywhere else are in Column 1, from B in Column 2 etc. So all we have to do is to somehow represent A as Row / Column 1 (Array Index 0), B as Row / Column 2 (Array Index 1) and so on. Using such a mapping will allow us to immediately find the necessary Row and Column and therefore pick out the Value.

That's where the ListBox (or ComboBox) are so useful in this type of application. The first Item you add to the Box will be at ListIndex 0, the Second Item at ListIndex 1 etc. so that's how we map the Place name to a Number. If we add Place A as the first item, when the user clicks on A, the ListIndex property will be 0 which just happens to be the Array Index of the Row / Column number of all the distances from A and so it goes on.

You could still use the example code I posted for your 31 X 31 places. You'd need to increase the number of elements in the array to (30,30) and your data would look like

Place A, Place B, Place C, Place D,....................................... Place N
Place A, Distance to A, Distance to B, Distance to C.......Distance to N
Place B, Distance to A, Distance to B, Distance to C.......Distance to N
Place C, Distance to A, Distance to B, Distance to C.......Distance to N
Place D, Distance to A, Distance to B, Distance to C.......Distance to N

through to

Place N, Distance to A, Distance to B, Distance to C........Distance to N

Then you could put a check in the LstTo_Click Event to check if the From and To places were the same (ie intFrom = lstTo.ListIndex)

Hope that's clear enough.


[Edited by TheGoat on 16-12-2007 at 08:31 AM GMT]

[Edited by TheGoat on 16-12-2007 at 08:37 AM GMT]

[Edited by TheGoat on 16-12-2007 at 08:47 AM GMT]

16-12-2007 at 08:27 AM
View Profile Send Email to User Show All Posts | Quote Reply
mrcoolcurrent
Level: Big Cheese

Registered: 19-01-2007
Posts: 23
icon Re: Milage calculator

Thank ypu so much. I just read your code & I think its just what I been looking for & I been reading about arrays but this is the first time I grab the idea.

I think I got the concept.

But my question now how am I gonna use or manipulate the array? I mean what am I going to use as a pointer.

I know listindex. How could I use it as a pointer to my arrays.
Because I have to combo boxes which means I have two listindexes.

But as I am typing ideas are flowing into my head & I think I need to try & experiment.

You really put me on the way & I am thinking and confident I could do it.

You know its like a baby trying to take the first step.

But I will do i.

I will get back to you.!

16-12-2007 at 06:20 PM
View Profile Send Email to User Show All Posts | Quote Reply
mrcoolcurrent
Level: Big Cheese

Registered: 19-01-2007
Posts: 23
icon Re: Milage calculator

my problem is not yet solved. I need help

19-12-2007 at 07:54 PM
View Profile Send Email to User Show All Posts | Quote Reply
TheGoat
Level: Protégé

Registered: 13-12-2007
Posts: 4
icon Re: Milage calculator

What part are you having problems with ?

20-12-2007 at 06:39 AM
View Profile Send Email to User Show All Posts | Quote Reply
mrcoolcurrent
Level: Big Cheese

Registered: 19-01-2007
Posts: 23
icon >=( Re: Milage calculator

My problem is getting further from where you left me. I understand the concept of arrays and how it can be implemented in my project. My problem is implementing it so let me take you through my project.

I have  2 comboboxes  the FROM and TO comboboxes.



The FROM combobox is named cboFrom and the To combobox is named cboTo.

I have cities which could be refrenced by the Listindex property.

Like :

City Listindex
A -1
B 0
C 1
D 2
E 3

e.t.c

My Idea is to use the list index property to find and return an element from the array like this



       A     B     C     D     E
A     0    10     20   30   40

B    10    0      15   20   30

C    20   15     0     25   30

D    30   20    25    0     40

E    40   30    30    40    0

So that Listindex -1 of cboFrom  and Listindex -1 of cboTo  would represent 0 miles and  that Listindex -1 of cboFrom  and Listindex 0 of cboTo  would represent 10 miles, that Listindex -1 of cboFrom  and Listindex 1 of cboTo  would represent 20 milage ……..


2. How can I write multi dimensional array to for the Listindex of  comboboxes of to and from. I mean like


Array  
Dim cbolistindex -1 to 30, cboTolistindex -1 to 30 as interger

So that I can say ;
cbolistindex -1 , cboTolistindex -1=0
cbolistindex -1 , cboTolistindex  0 =10
cbolistindex -1 , cboTolistindex  1 =20


My idea is to use the listr index property to determine the x and y axis of my matrix in order to return a value stored there.

I hope you understand. Or is there a better way to do it.


This is what I tried but never worked . I have uploaded my form as a jpeg. Check it out & help me please.


Dim intFrom(30) As Integer
Dim intTo(30) As Integer
intFrom = cboFrom.ItemData(cboFrom.ListIndex)
intTo = cboTo.ItemData(cboTo.ListIndex)
Dim DISTANCE(intTo, intFrom)




'Computation for  distances TO location no 0

Private Sub cmdCalculate_Click()
  



DISTANCE(intTo0 , intFrom1) = 758
DISTANCE(intTo0 , intFrom2 )= 432
DISTANCE(intTo0 , intFrom3 )= 258
DISTANCE(intTo0 , intFrom4 )= 147
DISTANCE(intTo0 , intFrom5 )= 454
DISTANCE(intTo0 , intFrom6 )= 897
DISTANCE(intTo0 , intFrom7 )= 458
DISTANCE(intTo0 , intFrom8 )= 236
DISTANCE(intTo0 , intFrom9 )= 257
DISTANCE(intTo0 , intFrom10) = 214
DISTANCE(intTo0 , intFrom11 )= 785
DISTANCE(intTo0 , intFrom12 )= 478
DISTANCE( intTo0 , intFrom13 )= 259
DISTANCE(intTo0 , intFrom14 )= 266
DISTANCE(intTo0 , intFrom15 = 740)
  
  
  lblDistance.Caption = DISTANCE & " KM"
  
  
  
  
  
End Sub





Thanks






____________________________
Attached:
Distance.jpg (106 KB)

22-12-2007 at 08:25 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 929
icon Re: Milage calculator

Hi,
Your basis is incorrect.
You should load the array first (Regardless of combos), then use the combo to read from the array (not right to it).



' In the form declarations section, add this line
dim myarray(5,5) as integer


add this to form_load

' First row in matrix
myarray(0,0)=0
myarray(0,1)=10
myarray(0,2)=20
myarray(0,3)=30
myarray(0,4)=40

' Second row in matrix
myarray(1,0)=10
myarray(1,1)=0
myarray(1,2)=15
myarray(1,3)=20
myarray(1,4)=30



now, to find the distance based on index:

mydistance = myarray(me.combo1.listindex, me.combo2.listindex)

combos start at 0, -1 means the user entered something that wasnt recognised so change the style property of your combo to dropdown list (i think it's 2).

You can use some math to work out the array index - if all places are a single letter, by converting the letters into numeric equivalents (ASCII)

dim ascii_of_combo1 as integer
dim index_of_combo1 as integer
ascii_of_combo1 =asc(me.combo1.text)
' The letter A is 65, so subtract this to start from 0
index_of_combo1 = ascii_of_combo1 - 65

dim ascii_of_combo2 as integer
dim index_of_combo2 as integer
ascii_of_combo2 =asc(me.combo2.text)
' The letter A is 65, so subtract this to start from 0
index_of_combo2 = ascii_of_combo2 - 65

distance = myarray(index_of_combo1,index_of_combo2)


Hope this helps,
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

28-12-2007 at 09:38 AM
View Profile Send Email to User Show All Posts | Quote Reply
mrcoolcurrent
Level: Big Cheese

Registered: 19-01-2007
Posts: 23
icon [SOLVED] Re: Milage calculator

Thank You.

My PROBLEM IS SOLVED!!!

All along my problem has been I was putting this on General Declarations!




add this to form_load

First row in matrix
myarray(0,0)=0
myarray(0,1)=10
myarray(0,2)=20
myarray(0,3)=30
myarray(0,4)=40

' Second row in matrix
myarray(1,0)=10
myarray(1,1)=0
myarray(1,2)=15
myarray(1,3)=20
myarray(1,4)=30



02-01-2008 at 08:06 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Milage calculator Solved Topic
Previous Topic (Converting numbers to Words)Next Topic (import fields from XML file to ACCESS) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder