vb.net - Arrays in visual basic (Related to program that deals with driving distance) -
i have assignment must create project looks driving distance between 2 cities.
use 2 drop-down lists contain names of cities. label 1 list “departure” , other “destination”. use button calculate distance. store distances in two-dimensional table.
i started doing it, noticed having repeat code many times , know there better way it. that's need help.
is there way use loop in problem , if so, how it?
option strict on public class form1 dim distance(,) long = {{0, 1004, 1753, 2752, 3017, 1520, 1507, 609, 3155, 448}, {1004, 0, 921, 1780, 2048, 1397, 919, 515, 2176, 709}, {1753, 921, 0, 1230, 1399, 1343, 517, 1435, 2234, 1307}, {2752, 1780, 1230, 0, 272, 2570, 1732, 2251, 1322, 2420}, {3017, 2048, 1399, 272, 0, 2716, 1858, 2523, 1278, 2646}, {1520, 1397, 1343, 2570, 2716, 0, 860, 1494, 3447, 1057}, {1507, 919, 517, 1732, 1858, 860, 0, 1307, 2734, 1099}, {609, 515, 1435, 2251, 2523, 1494, 1307, 0, 2820, 571}, {3155, 2176, 2234, 1322, 1278, 3447, 2734, 2820, 0, 2887}, {448, 709, 1307, 2420, 2646, 1057, 1099, 571, 2887, 0}} private sub lookupbtn_click(byval sender system.object, byval e system.eventargs) handles lookupbtn.click if combobox1.selectedindex = 0 , combobox2.selectedindex = 0 distancelbl.text = (distance(0, 0).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 1 distancelbl.text = (distance(0, 1).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 2 distancelbl.text = (distance(0, 2).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 3 distancelbl.text = (distance(0, 3).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 4 distancelbl.text = (distance(0, 4).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 5 distancelbl.text = (distance(0, 5).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 6 distancelbl.text = (distance(0, 6).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 7 distancelbl.text = (distance(0, 7).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 8 distancelbl.text = (distance(0, 8).tostring & " miles") elseif combobox1.selectedindex = 0 , combobox2.selectedindex = 9 distancelbl.text = (distance(0, 9).tostring & " miles") end if end sub end class
it's noticed repetition , thought loops, don't need 1 problem.
you're doing equivalent of:
- if x=0 , y=0 @ array(0,0)
- if x=0 , y=1 @ array(0,1)
- ...
- if x=9 , y=9 @ array(9,9)
which can expressed more as:
- look @ array(x,y)
therefore, real code, it's simple this:
distancelbl.text = (distance( combobox1.selectedindex, combobox2.selectedindex).tostring & " miles")
Comments
Post a Comment