Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I declared Dim arr1(2) as string

I need to change array size based on some input.

Eg. I want to change array size from 2 to 4, what method would I need to use?
Posted
Updated 4-Jul-11 20:49pm
v2
Comments
Dalek Dave 5-Jul-11 2:49am    
Edited for Grammar, Syntax and Readability.

Arrays are poorly designed for re-size. Instead, if you need anything with variable size, always use System.Collections.Generic.List<string> (or any other element type); you can get back to array using its method ToArray.

—SA
 
Share this answer
 
Comments
Abhinav S 5-Jul-11 3:53am    
Good advice. my 5.
Sergey Alexandrovich Kryukov 5-Jul-11 4:07am    
Thank you, Abhinav.
--SA
Simon_Whale 5-Jul-11 8:15am    
sound advice +5
Sergey Alexandrovich Kryukov 5-Jul-11 12:55pm    
Thank you, Simon.
--SA
Try Array.Resize(arr1, 4).
 
Share this answer
 
Comments
Nathansathya 5-Jul-11 2:45am    
Tks lot Abinav its works......
Abhinav S 5-Jul-11 2:52am    
You are welcome.

You might want to read about the preserve keyword as well.
Dalek Dave 5-Jul-11 2:50am    
That is how I would do it.
Abhinav S 5-Jul-11 2:52am    
Thanks DD.
Toniyo Jackson 5-Jul-11 2:59am    
This also correct +5
Redim arr1(4) as string
' if u r trieng to preserve previous data than 
Redim preserve arr1(4) as string
 
Share this answer
 
Comments
Nathansathya 5-Jul-11 2:45am    
tks sahu........
Toniyo Jackson 5-Jul-11 2:49am    
Good answer +5
Dalek Dave 5-Jul-11 2:49am    
Good Call.
Abhinav S 5-Jul-11 2:52am    
Thanks DD.
Sahu.Ashok 5-Jul-11 3:19am    
Thanks Tonyo, Dave and welcome Abhinav & Nathansathys
Hello,

define a static variable count and the define your array with count set the count's value that will be your array size.

Hoping it will help you
Thanks
sanjeev
 
Share this answer
 
Comments
Nathansathya 5-Jul-11 2:45am    
thanks sanjeev........
Dalek Dave 5-Jul-11 2:50am    
Helpful.
[no name] 5-Jul-11 2:51am    
thanks Dave
you can use 'ReDim Preserve' keyword

suppose you have declared Dim arr1(2) as string, and now you can increase size of arr1 then you can use 'redim preserve' like this

'ReDim Preserve arr1(8)'

for more information aabout Redim statement you can visit
http://www.authorcode.com/redim-statement-of-array-in-vb-net/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900