-2

So the issue is

"Create a project that looks up the driving distance between two cities. Use two drop-down lists that contain the names of the cities. Label one list Departure and the other Destination. Use a Look Up button to calculate the distance. Store the distances in a two-dimensional table."

is there a better way of coding this by using a for loop

ok here is the code below

    private void lookUpButton_Click(object sender, EventArgs e)
    {


          int [,] miles = { {0,1004, 1753, 2752, 3017,1520, 1507,609, 3115,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 }

        };

                    //var distance = miles[txtDeparture.SelectedIndex,txtDeparture.SelectedIndex];
        // alot of if statments 

        if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 0))
        {

            var distance = miles[0, 0];

            txtDistancebox.Text = distance.ToString();

        }
        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 1))
        {

            var distance = miles[0, 1];

            txtDistancebox.Text = distance.ToString();
        }

        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 2))
        {

            var distance = miles[0, 2];

            txtDistancebox.Text = distance.ToString();


        }
        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 3))
        {

            var distance = miles[0, 3];

            txtDistancebox.Text = distance.ToString();


        }

        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 4))
        {

            var distance = miles[0, 4];

            txtDistancebox.Text = distance.ToString();


        }
        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 5))
        {

            var distance = miles[0, 5];

            txtDistancebox.Text = distance.ToString();


        }
        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 6))
        {

            var distance = miles[0, 6];

            txtDistancebox.Text = distance.ToString();


        }
        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 7))
        {

            var distance = miles[0, 7];

            txtDistancebox.Text = distance.ToString();


        }

        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 8))
        {

            var distance = miles[0, 8];

            txtDistancebox.Text = distance.ToString();


        }
        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 9))
        {

            var distance = miles[0, 9];

            txtDistancebox.Text = distance.ToString();


        }

        else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 10))
        {

            var distance = miles[0, 10];

            txtDistancebox.Text = distance.ToString();


        }

        else if ((txtDeparture.SelectedIndex == 1) && (txtDestination.SelectedIndex == 0))
        {

            var distance = miles[1, 0];

            txtDistancebox.Text = distance.ToString();


        }



    }
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
Super_Fly
  • 115
  • 9
  • If your code works and you're simply looking for suggestions to improve it, your post belongs on [codereview.se] instead. – Ken White Oct 25 '18 at 00:41
  • 1
    You should see a pattern in your code and realize you can replace your hard-coded index values with the objects in your `if` condition... – Rufus L Oct 25 '18 at 00:43
  • Also, the `txt` prefix is normally used for a TextBox, not a ComboBox… – Rufus L Oct 25 '18 at 00:47
  • Also you might consider storing your array at the class level rather than recreating it each time the button is clicked. – Rufus L Oct 25 '18 at 00:49
  • @Rufus OP's question 2 hours ago also had the same answer. I wonder why OP has posted it a second time. – ProgrammingLlama Oct 25 '18 at 00:54

2 Answers2

1

Something like this:

var distance = miles[txtDeparture.SelectedIndex, txtDestination.SelectedIndex];
txtDistancebox.Text = distance.ToString();
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
0

I think you can make one method for calculating txtDistancebox. like as ...Private int CalculateDistace(int DepartureIndex, int DestinationIndex)

  • But there is no calculation needed...it's just looking up an item in the array using the two indexes. How would this `CalculateDistace` method help in the code sample above? – Rufus L Oct 25 '18 at 00:48
  • My thought was short. I agree your think!! – young hwa seo Oct 25 '18 at 00:50