471,348 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

C# counterpart to VB.NET Redim

Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas
Feb 10 '06 #1
4 1655
"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas


You need to reallocate and copy yourself. To can use the array's Copy(To)
method for this purpose.

If you don't want to do this, just use an ArrayList instead of an array and
just Add() elements all you want and let the collection take care of things
for you.

m
Feb 10 '06 #2
Additionally, in .NET 2.0, you can just use the Generic type List<T>
which will do this as well and give you type safety.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike" <vi********@yahoo.com> wrote in message
news:Og**************@TK2MSFTNGP15.phx.gbl...
"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas


You need to reallocate and copy yourself. To can use the array's Copy(To)
method for this purpose.

If you don't want to do this, just use an ArrayList instead of an array
and just Add() elements all you want and let the collection take care of
things for you.

m

Feb 10 '06 #3
First of all, a .NET collection such as ArrayList might be preferable.
However, if you want to resize an array, then the following is what you need:
(assuming an array of type "SomeType")

SomeType[] temp = new SomeType[11];
if (A != null)
Array.Copy(A, temp, Math.Min(A.Length, temp.Length));
A = temp;
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Niklas" wrote:
Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas

Feb 11 '06 #4
List<T> is dynamic, so you don't even need to redim.

--
William Stacey [MVP]

"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
| Hi
| What is the best way to enlarge an C# array which already have values?
| In VB.NET I would do
| ReDim Preserve A(10)
| regards
| /Niklas
Feb 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Trevor Fairchild | last post: by
2 posts views Thread by Wayne Wengert | last post: by
9 posts views Thread by John A Grandy | last post: by
4 posts views Thread by Han | last post: by

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.