Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to increment an item array by 1

Status
Not open for further replies.

pinkpoppy

Programmer
Jul 6, 2011
75
0
0
US
I have an item array: A600

I need to increase it by 1 (A601). How do I increase it and have it shown in a textbox?

Thanks
 
If you need to be able to change the size you should really be using a List.

Eg, instead of:

Code:
string[] ar = new string[600];
ar[0] = "blah";
ar[1] = "blah blah";
....

You could do:

Code:
List<string> ar = new List<string>();
ar.Add("blah");
ar.Add("blah blah");

A list uses an array to store its data, so you get the speed of an array with the added benefit of dealing with a linked list.. which means you can add and remove items without worrying about the size.


 
Plus, you can still access the elements of the List like you would the array. In the second example, ar[1] would still return "blah blah" like in the first example.
 
Keep in mind how one vs. the other works. An Array.Resize actually copies the existing array into a new array. While the List does this as well, it actually maintains excess capacity and then essentially tracks which index you're on. It only resizes once full, doubling capacity each time it does.

If you're dealing with large sets of information where the array is going to need to expand (or contract) on the fly, you're considerably better off using a list; using an array resize each time you need to add a new index is considerably less efficient. If it's just a one off or you're dealing with small sets of data, I suppose it's fine, but you really should just use a List.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top