A practice exercise requires the creation of a text file with 4 lines of info for cars, VIN, model, manufacturer, and year repeated over and over for different vehicles. This I do with StreamWriter and have a simple multiline text file as follows:

111111
Echo
Toyota
2005
222222
Accord
Honda
2001

etc.(VINs simplified)

.....each line being read from a specific textBox on the form with .WriteLine.
This is simple and straight forward.

The next part of the exercise requires a form on which the VIN numbers are loaded into a comboBox and when a certain VIN is selected, the rest of the car info is displayed in a control on the form.

I cannot grasp the programming logic required to do this.
The best I've come up with is a StreamReader to read the saved file into an array.

For just the two simple entries above I use...

StreamReader fileReader = new StreamReader("C:\\car.txt");
string[,] carArray = new string[2,4];
// Load the array.
for(int rowi = 0; rowi < 2; rowi++)
{
  for(int coli = 0; coli < 4; coli++)
    {
      carArray[rowi, coli] = fileReader.ReadLine();
    } 
}

I then load my combobox list of the VINs directly from the subscript like so:

comboBox1.Items.Add(carArray[0,0]); 
comboBox1.Items.Add(carArray[1,0]);

I can then use the selectedItem.ToString() literal text value of the combo box to load
a list box to display the other relevent info pertaining to that VIN, again by direct subscript reference.

if(comboBox1.SelectedItem.ToString() == "111111");
{
    listBox1.Items.Add(array[0, 1]);
    listBox1.Items.Add(array[0, 2]);
    listBox1.Items.Add(array[0, 3]);    
}

I do a similar thing, with a listBox1.Clear(); preceeding the .Add statements to display the next array row for the Accord.

This code works fine for two cars but is very impractical for a long listing of cars.

Of course, a single dimension array of [8] will work as well when done this way.

I would very much appreciate suggestions on a better approach to this exercise bearing in mind the single line structure of the text file. the VINs must be in the comboBox list, and the related car info is to be displayed in a listBox for the selected VIN of the comboBox. Thanks to anyone with any suggestions.

Recommended Answers

All 5 Replies

Like you said, for long listing of cars it is impractical, but not because of your algorithm, because it would be impractical to have it on a Text file.

Maybe there are better ways of doing what you just did, but it's still a text file, so without putting a lot of thought into it, I also don't see a better way other than storing the file in memory.

For large files you could use a Buffer and start loading the file into memory in chunks.

Even an <XML> structure would be better here. Then you'd be able to access the nodes directly.

Thanks charlybones.
I agree with you on the better idea of storing into memory. Unfortunately the exercise requires two projects. The first to store the simple data into a text file line by line from textBoxes on a form. This can be done with File class or Stream class.
Now, given the text file of single lines of data, the second project must "load the data from the file into memory and load a comboBox" with one specific line of the file, namely,the VINs which repeat every 4th line in the file. "When a VIN is selected from the comboBox, display the appropriate information regarding the vehicle" i.e. the other 3 lines of data.
I've been struggling with this now for a long time and just can't seem to grasp how to achieve this.
I can load the file to an array with StreamReader and a loop but do not know how to load only the VIN line into the comboBox list and reference the other appropriate data from a selected VIN.
The primary reason I chose the two dimensional array was to see if I could do an indirect lookup like a record of a database since the info in the array would look like this:
111111 Echo Toyota 2005
222222 Accord Honda 2001
........and by selecting the VIN field I could somehow show the whole row or record.
I'm now thinking maybe a LINQ query of the array may be the way to go.
So, if I input the actual 17 digit VIN number, maybe this would work:

var vinQuery =
    from aString in carArray
    where aString.Length == 17
    select aString;
comboBox1.DataSource = vinQuery.ToList();

At least this gets me to the point of having the comboBox list loaded with the VINs.
......but much more work to do on this yet1

Just to throw this out there: what if you read all the data for one car into a List<string> and added that to a List<List<string>>?

Add the first member of each list (the VIN) to the combobox, then use the SelectedIndex of the combobox as your index into the List<List<>> and display the make, model, etc.

I would like to see a sample of the list of a list code ... too much for my level.
But many thanks jonsca.

Nearly anything is too much for my level - hence the trouble.

What I have come up with so far is this, thanks to some much needed help from this forum.
This is done with simpler text files but the idea is to write 2 text files when inputing the data which in this example is name and name & phone number.

name.txt =
Bill
Sue
Dave

number.txt =
Bill - 708 344-7865
Sue - 608 333-5641
Dave - 456 222-4554

Coded into the Form_Load event of the lookup form is a declared Struct with the two fields. Two fields of the Struct are filled with the file data via StreamReader. A display button click event cross references the selected index, name, of a combobox and displays the related info, number, in a richtextbox.
Probably very lame code I know.

public partial class Form1 : Form
{
  public struct Person
  {
     public string name {get; set;} 
     public string number {get; set;}
  }
Person[] arrayOfPerson = new Person[3];
private void Form1_Load(object sender, EventArgs e)
 {
  string[] myArray = new string[3];
  string[] myArray2 = new string[3];
  StreamReader fileReader = new StreamReader("name.txt");
  StreamReader fileReader2 = new StreamReader("number.txt");
  string fileString = "", fileString2 = "";
  int i = 0, i2 = 0;
  while(fileString != null && fileString2 != null)
  {
    fileString = fileReader.ReadLine();
    fileString2 = fileReader2.ReadLine();
    if(fileString != null && fileString2 != null)
    {
      myArray[i++] = fileString;
      myArray2[i2++] = fileString2;
    }
  }
  for(int i = 0; i < arrayOfPerson.Length; i++)
  {
    arrayOfPerson[i].name = myArray[i];
    arrayOfPerson[i].number = myArray2[i];
  }

  private void displayButton_Click(object sender, EventArgs e)
  {
    int indexInt;
    if(comboBox1.SelectedIndex != -1)
    {
      indexInt = comboBox1.SelectedIndex;
      richTextBox1.Text = arrayOfPerson[indexInt].number.ToString();
    }
  }
 }

Disadvantages of this code is that the arrays are static although the array of the struct can be sized at any number less than the other arrays.

It's not lame, everyone is still learning something or other... :)

Lists are not too hard to add to your repertoire, and they are very useful when you don't know how many items you have.

List<string> sl = new List<string>();
sl.Add("first");
sl.Add("second");
sl.Add("third");

//sl[0] gives you "first"
//sl[1] gives you "second"
//sl[2] gives you "third"

You can make a list of your structs,too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.