473,387 Members | 1,549 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,387 software developers and data experts.

Is there a way to ReDim in C#?

mb
I have an array that starts with 99 index. I use a random amount of the
indexes, let's say 23. Is there a way to truncate the array to 23 elements
while preserving the 23 used? The ReDim statement in VB does this. Is
there an equivalent in C#?
Jul 21 '05 #1
7 14623

"mb" <mm@hotmail.com> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
I have an array that starts with 99 index. I use a random amount of the
indexes, let's say 23. Is there a way to truncate the array to 23
elements
while preserving the 23 used? The ReDim statement in VB does this. Is
there an equivalent in C#?

Not explicitly. You can use something like this to get equivilent
functinoality however:

Array ResizeArray(Array array, Type type, params int dimensions)
{
Array newArray = Array.CreateInstance(type, dimensions);
Buffer.BlockCopy(array, 0, newArray, 0, array.Length);
return newArray;
}

Which will do the basic job for you(its untested, fyi). It appears
System.Array gets a Resize<T> function in whidbey as well, but that has to
wait, ;).

Jul 21 '05 #2
MB,

Luckily not, when you have to do something with a redim, than the best you
can do is to change that part of your upgraded VB6 program in VBNet as well
direct with an array which uses IList or Icontainer, the most properiate is
mostly the arraylist in that case.

I hope this helps?

Cor
Jul 21 '05 #3
As Daniel & Cor already mentioned.. luckily there is not ReDim in C#. Either
write a function that populates the new array with values from the old one
or consider using box standard ArrayList object. You can choose to specify
the size or you can leave it to take however many items you feed in.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"mb" <mm@hotmail.com> wrote in message
news:u#**************@TK2MSFTNGP09.phx.gbl...
I have an array that starts with 99 index. I use a random amount of the
indexes, let's say 23. Is there a way to truncate the array to 23 elements while preserving the 23 used? The ReDim statement in VB does this. Is
there an equivalent in C#?

Jul 21 '05 #4
mb
Thank you.

"Hermit Dave" <he************@CAPS.AND.DOTS.hotmail.com> wrote in message
news:ej*************@TK2MSFTNGP09.phx.gbl...
As Daniel & Cor already mentioned.. luckily there is not ReDim in C#. Either write a function that populates the new array with values from the old one
or consider using box standard ArrayList object. You can choose to specify
the size or you can leave it to take however many items you feed in.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"mb" <mm@hotmail.com> wrote in message
news:u#**************@TK2MSFTNGP09.phx.gbl...
I have an array that starts with 99 index. I use a random amount of the
indexes, let's say 23. Is there a way to truncate the array to 23

elements
while preserving the 23 used? The ReDim statement in VB does this. Is
there an equivalent in C#?


Jul 21 '05 #5
Other people have mentioned the ArrayList, please note that this operates on
'object'.
If you have valuetypes like int or double, you will get boxing/unboxing.
This may not cause a performance problem, but it's worth knowing about.

Chris
"mb" <mm@hotmail.com> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
I have an array that starts with 99 index. I use a random amount of the
indexes, let's say 23. Is there a way to truncate the array to 23
elements
while preserving the 23 used? The ReDim statement in VB does this. Is
there an equivalent in C#?

Jul 21 '05 #6
As Chris mentioned keep in mind that ArrayList is very generic.. using
object type so you will incure boxing / unboxing won't have a much
performance effect but its good to know.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"mb" <mm@hotmail.com> wrote in message
news:uN**************@TK2MSFTNGP09.phx.gbl...
Thank you.

"Hermit Dave" <he************@CAPS.AND.DOTS.hotmail.com> wrote in message
news:ej*************@TK2MSFTNGP09.phx.gbl...
As Daniel & Cor already mentioned.. luckily there is not ReDim in C#.

Either
write a function that populates the new array with values from the old one or consider using box standard ArrayList object. You can choose to specify the size or you can leave it to take however many items you feed in.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"mb" <mm@hotmail.com> wrote in message
news:u#**************@TK2MSFTNGP09.phx.gbl...
I have an array that starts with 99 index. I use a random amount of the indexes, let's say 23. Is there a way to truncate the array to 23

elements
while preserving the 23 used? The ReDim statement in VB does this. Is there an equivalent in C#?



Jul 21 '05 #7
As the others have mentioned, you may want to rethink your approach in
.NET.

However, there is a 4-line equivalent in C# which does what VB.NET
does behind the scenes (performance is identical), as our Instant C#
VB.NET to C# converter shows:

VB.NET:

Sub TestReDim()
Dim x() As Integer
ReDim x (2)
ReDim Preserve x(3)
End Sub

C#:

private void TestReDim()
{
int[] x = null;
x = new int [3];
//INSTANT C# NOTE: The following 4 lines reproduce what 'ReDim
Preserve' does behind the scenes in VB.NET:
//ORIGINAL LINE: ReDim Preserve x(3)
int[] Temp1 = new int[4];
if (x != null)
System.Array.Copy(x, Temp1, System.Math.Min(x.Length,
Temp1.Length));
x = Temp1;
}

Jul 21 '05 #8

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

Similar topics

4
by: Trevor Fairchild | last post by:
I've got a program that parses text files. The text files come to me in Unicode and they contain goofy characters that VB chokes on - treats them as eof markers. I have already been through this...
2
by: Wayne Wengert | last post by:
I am trying to add one column to an existing array (code below). The ReDim command gives the error: ----------------------------------------------- Microsoft VBScript runtime error '800a0009' ...
2
by: | last post by:
Is it correct to think that after reducing the populated array's size from say, 10 to 5 with redim preserve myArray(i) an attempt to access an element above the fifth does not cause a...
4
by: Daryl Davis | last post by:
I am having trouble with ReDim (see code below) SaleTable2's structure includes an array for SaleDetailTable2 Dim newsale As New hallsales.SaleTable2 Dim detail As New hallsales.SaleDetailTable2...
5
by: Zenobia | last post by:
Hello, I want to keep a list references to database records being accessed. I will do this by storing the record keys in a list. The list must not contain duplicate keys. So I check the...
9
by: John A Grandy | last post by:
In VB6 you could get away with the following code: Dim Index As Integer Dim ItemsCount As Integer Dim StringArray() As String Dim StringValue As String '....
6
by: Adrian | last post by:
Hi In VB 6 I would declare an array in the general part to make it visible to all parts then once I know how elements I had I would redim it with the amount thus Dim testarray() as string ...
5
by: Paul | last post by:
Off the cuff, does anyone know if arraylist is more efficeint at adding items to an array than redim preserve? Paul <begin loop> Dim c As Integer = SomeArray.GetUpperBound(0) + 1 ReDim...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
2
by: Fredrik Strandberg | last post by:
I have not been able to find the solution of this problem anywhere: I am building a class PrivateHelper that provides methods to access private members and invoke private methods, to be used for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.