473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I add items to an array while the array's index is growing

JnrJnr
88
I'm going to leave out most details so say for instance i have a combobox and when I select a car from the combobox I have a variable called Index that grows with each selection of the combobox. that variable is the index of my array called myArray. So when the combobox selects then the index of the array grows. There is also a SQL select statemant that retreives a value called myCar.

Expand|Select|Wrap|Line Numbers
  1. string[] myArray;
Expand|Select|Wrap|Line Numbers
  1. int Index;
private void Combobox_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand getCar = new SqlCommand("select Car from myTable where carID = '"+ 3 +"' ", myConnection);[/code]
[code]myCar = getCar.ExecuteNoneQuery();

Index ++;
myArray = new string[Index];
MyArray[Index] = myCar.ToString();
}

The array obviously gets all it's indexes but only the last index in the array has the value of myCar and the rest is null.
The reason is because I'm declaring the array over and over each time with the new keyword.

How can I keep all the 'myCars' in the Array everytime the array's index grows?

Your Help would be appreciated
Sep 20 '10 #1

✓ answered by Anton Zinchenko

Instead of creating a new array each time, use
Expand|Select|Wrap|Line Numbers
  1. Array.Resize(ref array, newsize)
. Or you can use a collection:

Expand|Select|Wrap|Line Numbers
  1. List<string> myCollection = new List<string>();
  2. private void Combobox_SelectedIndexChanged(object sender, EventArgs e)
  3. {
  4. SqlCommand getCar = new SqlCommand("select Car from myTable where carID = '"+ 3 +"' ", myConnection);
  5. myCar = getCar.ExecuteNoneQuery();
  6.  
  7. myCollection.Add(myCar.ToString());
  8. }

2 1411
Instead of creating a new array each time, use
Expand|Select|Wrap|Line Numbers
  1. Array.Resize(ref array, newsize)
. Or you can use a collection:

Expand|Select|Wrap|Line Numbers
  1. List<string> myCollection = new List<string>();
  2. private void Combobox_SelectedIndexChanged(object sender, EventArgs e)
  3. {
  4. SqlCommand getCar = new SqlCommand("select Car from myTable where carID = '"+ 3 +"' ", myConnection);
  5. myCar = getCar.ExecuteNoneQuery();
  6.  
  7. myCollection.Add(myCar.ToString());
  8. }
Sep 21 '10 #2
JnrJnr
88
Thanx Anton, Resizing the array works for me!
Bytes is awesome, Replies are more awesome
Sep 21 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: JT | last post by:
how do i determine how many items are in an array? the following code creates an array of values each time a space is found in a name field. the problem is that sometimes names have middle...
1
by: Nicolas Pernetty | last post by:
Hello, I'm trying to find a clear and fast equivalent to the index method of plain python list : >> a = >> a.index(4) 2 I have to use it on a Numeric array, so the best I've come up with...
6
by: Yanhao Zhu | last post by:
Hi, all, If I have an array like int m = new int { 0, 1, 2, 3 }, is there a way I can separate the array into two, like int m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items in...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
7
by: John | last post by:
Hi Is there a way to create an array of strings of subscript of type index too? Like; x("Element A")="Value for element A" Thanks Regards
7
by: Peter | last post by:
I want to create a multidemensional arraylist. Seeing as they don't exist I was wondering if there is a way to create a class that works like one. I basically want to use it like this Dim...
1
by: Daniel Gormley | last post by:
What I have is a form that is dynamically generated based on which database table its calling. Therefore, the number of category.name.count can be different. So I have this form generated and...
3
by: Robert Bevington | last post by:
Hi all, I ran into memory problems while tying to search and replace a very large text file. To solve this I break the file up into chunks and run the search and replace on each chunk. This...
21
by: aatif | last post by:
Hello, I have a no. of checkboxes on a c# form application. these are labelled as 1, 2, 3 and so on... I want to programmatically make them checked or unchecked according to their label, e-g; if...
0
by: larryzboy | last post by:
//~--- JDK imports ------------------------------------------------------------ import java.text.*; import java.io.Console; import java.util.regex.*; import java.util.Arrays; public class...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.