locked
Array input RRS feed

  • Question

  • So, I've been flipping around my book find the answer, that having 2 1/2 months of doesn't exactly help. So for Code below i need have way call the array and use it in the txtMessage.Text i have the {0} which i believe is correct, but I might be wrong. I need bit help how do I get the Strings like Name Velocity and such to be called into it. I was sure it had to do at end of the " ", Names but that doesn't seem to work get an error. Any help would be appreciated. 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace HW1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     /* main program */
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string[] Names = { "A-727", "A-737", "C-150", "D-240" };
                int[] TakeoffVelocity = { 250, 264, 270, 240 };
                double[] Acceleration = { 33.5, 44.2, 37.1, 51.9 };
                string aircraft = listBox1.SelectedItem.ToString();                                                                   /* the choices/result of the List box */
                if (aircraft == "A-727")                                                                                                           {
                    txtMessage.Text = " The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}. Therefore it requires  seconds to take of with a distance of  feet.";
                }

            }
        }
    }


    • Edited by Ruick78 Friday, January 22, 2016 6:59 PM
    Friday, January 22, 2016 6:58 PM

Answers

  • A bit unsure of question, but because the way the  thread is called "Array Input"

    Make me think that you're unsure as to how to access values in the array.

    Andy Oneill did demonstrate how to do so, but just to be a bit more in depth

    I see you have multiple arrays declared and I'm sure you want to choose the correct item in the arrays that corresponds to the selected item in the listbox.

    To do that, I would recommend getting the selected index instead of the selected item, that way you can just use that index to select the item of which you wanted for each array.

    Code would be as follows.

            private string[] _names = {
                                          "A-727",
                                          "A-737",
                                          "C-150",
                                          "D-240"
                                      };
    
            private int[] _takeoffVelocity = {
                                                 250,
                                                 264,
                                                 270,
                                                 240
                                             };
    
            private double[] _acceleration = {
                                                 33.5,
                                                 44.2,
                                                 37.1,
                                                 51.9
                                             };
    
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int index = this.listBox1.SelectedIndex;
    
                txtMessage.Text =
                    string.Format(
                                  " The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}. Therefore it requires  seconds to take of with a distance of  feet.",
                                  this._names[index],
                                  this._takeoffVelocity[index],
                                  this._acceleration[index]);
            }

    If the arrays are not going to be changed at all, it's best to declare them at the class scope that way you're not re-declaring them each time.  It's better for performance and it's best for practice to do it this way.

    Also, you don't have to rename the fields to follow "_lowerCamelCase" but once again it's better for practice.

    But it is recommend that if they are declared at class scope that they stay private.  If you need for them to be publicly accessible, you can do so using properties.

    NOTE: Make sure that each value in the array is in the same order as they are in the other ones.  Meaning, being that "A-727" starts off the Names array, make sure that starting of the rest of the arrays are the values for that name.  In further detail, make sure the first value in _takeOfVelocity which is "250" corresponds to the "A-727", and same for the first value of the array _acceleration which is "33.5", make sure that corresponds to the "A-727".

    I hope I did not make this too confusing, sometimes I am bad at explaining things.  If you have any questions, feel free to ask them away.

    • Proposed as answer by Anonrate Friday, January 22, 2016 10:17 PM
    • Marked as answer by Kristin Xie Monday, February 1, 2016 9:39 AM
    Friday, January 22, 2016 10:16 PM

All replies

  • This bit

    txtMessage.Text = " The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}. Therefore it requires  seconds to take of with a distance of  feet.";


    Looks like you've missed out the string.format and variables.

    https://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    So you want something like

    txtMessage.Text = string.Format(" The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}. Therefore it requires  seconds to take of with a distance of  feet."
    , variable1, variable2);

    Where you're supplying 2 variables to be inserted where the {0] and {1} are.

    You'd so domething like:

    txtMessage.Text = string.Format(" The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}. Therefore it requires  seconds to take of with a distance of  feet."
    , aircraft , TakeoffVelocity[0] );

    But I'm not really clear what you're trying to do here.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles


    • Edited by Andy ONeill Friday, January 22, 2016 7:11 PM
    • Proposed as answer by Anonrate Friday, January 22, 2016 9:53 PM
    • Unproposed as answer by Anonrate Friday, January 22, 2016 9:59 PM
    • Proposed as answer by Anonrate Friday, January 22, 2016 10:00 PM
    • Unproposed as answer by Anonrate Friday, January 22, 2016 10:17 PM
    Friday, January 22, 2016 7:10 PM
  • A bit unsure of question, but because the way the  thread is called "Array Input"

    Make me think that you're unsure as to how to access values in the array.

    Andy Oneill did demonstrate how to do so, but just to be a bit more in depth

    I see you have multiple arrays declared and I'm sure you want to choose the correct item in the arrays that corresponds to the selected item in the listbox.

    To do that, I would recommend getting the selected index instead of the selected item, that way you can just use that index to select the item of which you wanted for each array.

    Code would be as follows.

            private string[] _names = {
                                          "A-727",
                                          "A-737",
                                          "C-150",
                                          "D-240"
                                      };
    
            private int[] _takeoffVelocity = {
                                                 250,
                                                 264,
                                                 270,
                                                 240
                                             };
    
            private double[] _acceleration = {
                                                 33.5,
                                                 44.2,
                                                 37.1,
                                                 51.9
                                             };
    
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int index = this.listBox1.SelectedIndex;
    
                txtMessage.Text =
                    string.Format(
                                  " The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}. Therefore it requires  seconds to take of with a distance of  feet.",
                                  this._names[index],
                                  this._takeoffVelocity[index],
                                  this._acceleration[index]);
            }

    If the arrays are not going to be changed at all, it's best to declare them at the class scope that way you're not re-declaring them each time.  It's better for performance and it's best for practice to do it this way.

    Also, you don't have to rename the fields to follow "_lowerCamelCase" but once again it's better for practice.

    But it is recommend that if they are declared at class scope that they stay private.  If you need for them to be publicly accessible, you can do so using properties.

    NOTE: Make sure that each value in the array is in the same order as they are in the other ones.  Meaning, being that "A-727" starts off the Names array, make sure that starting of the rest of the arrays are the values for that name.  In further detail, make sure the first value in _takeOfVelocity which is "250" corresponds to the "A-727", and same for the first value of the array _acceleration which is "33.5", make sure that corresponds to the "A-727".

    I hope I did not make this too confusing, sometimes I am bad at explaining things.  If you have any questions, feel free to ask them away.

    • Proposed as answer by Anonrate Friday, January 22, 2016 10:17 PM
    • Marked as answer by Kristin Xie Monday, February 1, 2016 9:39 AM
    Friday, January 22, 2016 10:16 PM
  • that helped out a lot thank you very much, I have one more question so i add more to program i need but i have odd issue with the print array for the names. Finished code below but when i run it, when goes each choice goes for aircaft name (probably something bit obverse im looking over) but if i do say A-727, I just get A for A-737 it end up being just - and so on, so curious know why and how i can fix it, i have picture of what gui looks like also attached if needing it.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace HW1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string[] Names = { "A-727", "A-737", "C-150", "D-240" };
                double[] TakeoffVelocity = { 250, 264, 270, 240 };
                double[] Acceleration = { 33.5, 44.2, 37.1, 51.9 };
                string aircraft = listBox1.SelectedItem.ToString();
                if (aircraft == "A-727")
                {
                    double time;
                    double distance;
                    time = TakeoffVelocity[0] / Acceleration[0];
                    distance = Acceleration[0] * time * time;
                    txtMessage.Text = string.Format(" The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}.\n Therefore it requires " + time + " seconds to take of with \n a distance of " + distance +" feet.", aircraft[0], TakeoffVelocity[0], Acceleration[0]);
                }
                if (aircraft == "A-737")
                {
                    double time;
                    double distance;
                    time = TakeoffVelocity[1] / Acceleration[1];
                    distance = Acceleration[1] * time * time;
                    txtMessage.Text = string.Format(" The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}.\n Therefore it requires " + time + " seconds to take of with \n a distance of " + distance + " feet.", aircraft[1], TakeoffVelocity[1], Acceleration[1]);
                }
                if (aircraft == "C-150")
                {
                    double time;
                    double distance;
                    time = TakeoffVelocity[2] / Acceleration[2];
                    distance = Acceleration[2] * (time * time);
                    txtMessage.Text = string.Format(" The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}.\n Therefore it requires " + time + " seconds to take of with \n a distance of " + distance + " feet.", aircraft[2], TakeoffVelocity[2], Acceleration[2]);
                }
                if (aircraft == "D-240")
                {
                    double time;
                    double distance;
                    time = TakeoffVelocity[3] / Acceleration[3];
                    distance = Acceleration[3] * (time * time);
                    txtMessage.Text = string.Format(" The {0} aircraft has a required takeoff velocity of {1} ft/sec and an acceleration constant of {2}.\n Therefore it requires " + time + " seconds to take of with \n a distance of " + distance + " feet.", aircraft[3], TakeoffVelocity[3], Acceleration[3]);
                }
            }
        }
    }


    Wednesday, January 27, 2016 6:28 PM
  • Doing this

    string aircraft = listBox1.SelectedItem.ToString();
    
    aircraft[0];

    is just going to get the the first char.

    This should do what you want it to do.

            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                double[] takeoffVelocity =
                {
                    250,
                    264,
                    270,
                    240
                };
    
                double[] acceleration =
                {
                    33.5,
                    44.2,
                    37.1,
                    51.9
                };
    
                var index = this.listBox1.SelectedIndex;
    
                var time = takeoffVelocity[index] / acceleration[index];
                var distance = acceleration[index] * Math.Pow(time, 2D);
    
                txtMessage.Text =
                    string.Format(
                                  "The {0} Aircraft has a required Takeoff Velocity of "
                                  + "{1}ft/sec and an Acceleration Constant of {2}\nTherefore it requires "
                                  + "{3} seconds to take of with\na distance of {4} feet.",
                                  this.listBox1.Items[index],
                                  takeoffVelocity[index],
                                  acceleration[index],
                                  time,
                                  distance);
            }

    Wednesday, January 27, 2016 7:23 PM