c# - Assigning custom objects to array with a for loop? -
New car ("Buick", 1932), new car ("Ford", 1932), car [] array = New = "New", "Ford", 1992), New Car ("Fiat", 1988) Car [] New Car ("Dodge", 1999), New Car ("Honda", 1977)};
I tried to do something like this:
for (int i = 0; i & lt; dtable.Rows.Count; i ++ ) {DataRow drow = dtable .Rows [i]; Arrangement [] = New car (Draw ["Make"]. Toasting (), Draw ["Year"]. Toasting ()); }
How do I add additional data to the array while looping through a dataset?
UPDATE1:
I went with the solution proposed by @ Roy.
// Create array, car length [] arrayOfCars = specify new car [dtable.Rows.Count]; {Detroau Draw = DeBel. For Intuit = 0; I & lt; dtable.Rows.Count; i ++) ROS [I]; // Assign a specific index to each car within the array (arrayoffers [i]) array of cars [ii] = new car (draw ["make"]. Toasting (), draw ["year"]. ); }
Once created, you can not add element to the array . Instead of using an array, a list & lt; Cars & gt; Use
will allow you to call it. Add elements to add.
For example:
// Create a list of car objects list & lt; Cars & gt; New Car ("Ford", 1992), New Car ("Fiat", 1988), New Car ("Buick", 1932), New Car ("Ford", 1932) = New list & lt; Cars & gt; New car ("Dodge", 1999), New Car ("Honda", 1977)};
You can then:
for (int i = 0; i
You can use listOfCars
as you can use an array, and can access elements by the index:
< Pre> car myCar = listOfCars [3];
You should have an array after calling ToArray () in the list and "Add to List", then create it:
/ /. .. Add as above ... car [arrayOfCars = listOfCars.ToArray (); // Creates an array from your list
Edit:
If you are just trying to allocate and create your array from your data tab , And are not going to need to add elements to it after its creation, you can use an array such as:
// Create an array, car with total length [ ] ArrayOfCars = Specify new car [dtable] Rows.Count]; {Detroau Draw = DeBel. For Intuit = 0; I & lt; dtable.Rows.Count; i ++) ROS [I]; // Assign a specific index to each car within the array (arrayoffers [i]) array of cars [ii] = new car (draw ["make"]. Toasting (), draw ["year"]. ); }
Comments
Post a Comment