473,503 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

redimension array

y
Hi,

I have an array with 2 elements and I would like to keep
the elements but redimension the array so that I can add
another value.

How do I do this.

I have tried this but, i think I have screwy logic.

Thanks
intRedimArray = arr_Selected.Length;
object tmp_arr = arr_Selected.Clone();
arr_Selected (string [])tmp_arr;
arr_Selected = new string[intRedimArray];

arr_Selected[intArrayCounter-1] = strToAdd;
tmp_arr= null;

the program will keep showing 1.
MessageBox.Show(arr_Selected.Length.ToString());
Thanks for your help,

y

Nov 15 '05 #1
5 8361
You might be better off using an ArrayList.

"y" <y@hotmail.com> wrote in message
news:09****************************@phx.gbl...
Hi,

I have an array with 2 elements and I would like to keep
the elements but redimension the array so that I can add
another value.

How do I do this.

I have tried this but, i think I have screwy logic.

Thanks
intRedimArray = arr_Selected.Length;
object tmp_arr = arr_Selected.Clone();
arr_Selected (string [])tmp_arr;
arr_Selected = new string[intRedimArray];

arr_Selected[intArrayCounter-1] = strToAdd;
tmp_arr= null;

the program will keep showing 1.
MessageBox.Show(arr_Selected.Length.ToString());
Thanks for your help,

y

Nov 15 '05 #2
"y" <y@hotmail.com> wrote in message
news:09****************************@phx.gbl...
intRedimArray = arr_Selected.Length;
object tmp_arr = arr_Selected.Clone();
arr_Selected (string [])tmp_arr;
arr_Selected = new string[intRedimArray];

arr_Selected[intArrayCounter-1] = strToAdd;
tmp_arr= null;

the program will keep showing 1.
MessageBox.Show(arr_Selected.Length.ToString());


This won't work because the array size for 'new string[intRedimArray]'
should be a compile time constant. You can use Array.CreateInstance instead.
Nov 15 '05 #3
y,
To redimension an array you need to create a new array, copy the existing
elements to the new array, start using the new array.

Something like:
string[] list;

int newLength = list.Length + 5;

string[] temp = new string[newLength];
list.CopyTo(temp, 0);

list = temp;

Note, if you are decreasing the size of the array, you can use Array.Copy
instead of CopyTo.

Array.Copy(list, 0, temp, 0, newLength);

Actually Array.Copy works for either increasing or decreasing the array. As
long as you don't try to copy more elements than what is in the source array
or the destination array.

Hope this helps
Jay

"y" <y@hotmail.com> wrote in message
news:09****************************@phx.gbl...
Hi,

I have an array with 2 elements and I would like to keep
the elements but redimension the array so that I can add
another value.

How do I do this.

I have tried this but, i think I have screwy logic.

Thanks
intRedimArray = arr_Selected.Length;
object tmp_arr = arr_Selected.Clone();
arr_Selected (string [])tmp_arr;
arr_Selected = new string[intRedimArray];

arr_Selected[intArrayCounter-1] = strToAdd;
tmp_arr= null;

the program will keep showing 1.
MessageBox.Show(arr_Selected.Length.ToString());
Thanks for your help,

y

Nov 15 '05 #4
This won't work because the array size for 'new string[intRedimArray]'
should be a compile time constant.


I'm affraid this statement is not quite correct....
Nov 15 '05 #5
"Val Savvateev" <1@2.com> wrote in message
news:ut***************@TK2MSFTNGP10.phx.gbl...
This won't work because the array size for 'new string[intRedimArray]'
should be a compile time constant.


I'm affraid this statement is not quite correct....


I'm affraid you're right... my C++ background is showing :)
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2757
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
5155
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
3463
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
55526
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
10198
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
10046
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
1
2855
by: Kurt Lange | last post by:
HI, how to redimension an array in c#, and preserve it's contents (without using arraylist). In visual basic it... Dim I_interfaceArray() as I_interface = new I_interface() {objInterface} ...
3
1881
by: Logan | last post by:
Greetings to all, i need to create a matrix (or something that make the job) to store 3 values per row to insert everything (there will be several rows) later in my database but i can´t find a way...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
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...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.