0

my problem probably has a simple solution, but I am just not grasping it. Here's the situation, I wish to register a vehicle through several characteristics that are input in textboxes and so on, by the user, and stored in an array, so that he can then look up a vehicle, or alter certain characteristics of said vehicle.

Breaking it down in two-steps:

  1. First the user selects a value from a numericupdown and clicks a button, therefore defining the size of the array in use. This I did with the following code, which I do not know if it is 100% correct:

int aSize = Convert.ToInt32(numericUpDown1.Value);
int[] Viaturas;
Viaturas = new int[aSize];

  1. Now, the user has several TextBoxes, DateTimePickers and Comboboxes, where he inputs the characteristics of the vehicle. Then he clicks on a button and all that information gets stored in an array, in a way that each vehicle has it's characteristics stored, so that he is then able to look 'em up.

Assuming the first point is OK, the second one is where I struggle, I have no idea how to code this. Any ideas?

Thanks in advance!

4
  • What does aSize represent? Is that the number of vehicles that you are storing, with all vehicles having the same attributes? Commented Aug 29, 2017 at 19:50
  • @ElementalPete Yes, aSize is the size of the array defined in the numericupdown by the user, which refers to the number of vehicles that it can store, with all vehicles having the same attributes yes! Commented Aug 29, 2017 at 19:53
  • Why would you want the user to have to resize the array with an UpDown? when they add a new vehicle why not just do Viaturas.Length + 1 ? Also you cant just reconstruct an array. You'll lose it. You have to copy it first. Commented Aug 29, 2017 at 19:56
  • I think you just answered your own question - you need to know the size of the array in advance to do it the way he is trying... Commented Aug 29, 2017 at 20:03

2 Answers 2

1

It sounds like you want to create an object to store all the data in.

public class Vehicle {
   public Vehicle(string make...) {
       Make = make;
       ...
   }
   public string Make;
   public string Model;
   public string Year;
   public string Color;
...
}

Then you can use a List to store all vehicles, it will handle the size of the array for you:

List<Vehicle> Vehicles = new List<Vehicle>();
Vehicles.Add(new Vehicle(textboxMake.Text, ...));

And access them like:

textboxMake.Text = Vehicles[0].Make;
Sign up to request clarification or add additional context in comments.

9 Comments

Yes, the access part I didn't know, and I did try to use a List, which works fine. But, can't it be done with an array or a Multi Dimensional Array?
@GonçaloDias Always use a List unless you "have to" use an array. More flexibility.
@LarsTech I know, I wish I could, but this is one of those "have to" use an array. I have the option of using two methods, one of which has to be an array. The List was my first thought and i got it working, the array one is puzzling me.
Even if you have to pass it into a method that expects an array, Lists have a .ToArray() method if you are using a newer version of .Net. You can create a list, and then call MyMethodThatExpectsAnArray(myList.ToArray());
Seems like we are missing information to be able to help you correctly. How is the array going to be presented to the End user? What is the array going into that locks you into using an array?
|
0

I agree with bwoogie - use a strongly typed object for this, and use a list if you can. This sample shows how to add a new vehicle when the user fills out the form and clicks a button. It has samples in there for either an array or a list. Note that both the array and the list can be passed into the same method, which expects an array of vehicles:

// you should be able to use a list...
List<Vehicle> list = new List<Vehicle>();

// or if you must use an array
Vehicle[] array; // initialize it like you do in your example
int arrayPosition = 0;

private void button1_Click(object sender, EventArgs e)
{
    // create an instance of a strongly typed object using your textboxes, etc.
    Vehicle v = new Vehicle();
    v.Make = textBoxMake.Text;
    v.PurchaseDate = dtpickerPurchaseDate.Value;
    v.Engine = comboBoxEngine.SelectedText;

    // add the strongly typed object to a list
    list.Add(v);

    // or if you must use an array
    array[arrayPosition] = v;
    arrayPosition++;

    // you can call a method that expects an array even if you are using a list
    DoStuffWithTheArray(list.ToArray());

    // or if you must use an array
    DoStuffWithTheArray(array);
}

private void DoStuffWithTheArray(Vehicle[] array)
{
    // expects an array of vehicles, but you can call it with a list or an array.
}

1 Comment

Thank you very much, I will try to implement this now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.