473,486 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

increase the size of the array

reb
Hi,

How do i increase the size of the array without losing the
data of that array in c#?

thanks
Nov 15 '05 #1
6 12117
Use an ArrayList. Arrays always are fixed - size in C#

GP

"reb" <gb***@yahoo.com> wrote in message
news:0b****************************@phx.gbl...
Hi,

How do i increase the size of the array without losing the
data of that array in c#?

thanks

Nov 15 '05 #2
Just to add the other way to do it is to reallocate the array to the new
size and copy the old arrays contents into the new one. Saying that I would
use the ArrayList approach anyway in a situation where I don't know the
number of elements in advance. If you have then finished the operation of
adding items to it then you can *convert* the ArrayList into a strongly type
array like so:

AType items[] =(AType[]) myArrayList.ToArray( typeof( AType ) );

Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl...
Use an ArrayList. Arrays always are fixed - size in C#

GP

"reb" <gb***@yahoo.com> wrote in message
news:0b****************************@phx.gbl...
Hi,

How do i increase the size of the array without losing the
data of that array in c#?

thanks


Nov 15 '05 #3
If you do this with small arrays, it's ok (I also do this in that case), but
if the Array rows image that the elements must be copied on each call. In
this case it would be better to use an ArrayList (or an own, strong typed,
collectionclass)

GP

"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Just to add the other way to do it is to reallocate the array to the new
size and copy the old arrays contents into the new one. Saying that I would use the ArrayList approach anyway in a situation where I don't know the
number of elements in advance. If you have then finished the operation of
adding items to it then you can *convert* the ArrayList into a strongly type array like so:

AType items[] =(AType[]) myArrayList.ToArray( typeof( AType ) );

Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl...
Use an ArrayList. Arrays always are fixed - size in C#

GP

"reb" <gb***@yahoo.com> wrote in message
news:0b****************************@phx.gbl...
Hi,

How do i increase the size of the array without losing the
data of that array in c#?

thanks



Nov 15 '05 #4
I guess the trouble is though what is the definition of small? I would err
on the side of type safety and only if a performance was shown to be a
problem would I consider an ArrayList being exposed. I know what you mean
though :-)

Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:uh****************@TK2MSFTNGP10.phx.gbl...
If you do this with small arrays, it's ok (I also do this in that case), but if the Array rows image that the elements must be copied on each call. In
this case it would be better to use an ArrayList (or an own, strong typed,
collectionclass)

GP

"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Just to add the other way to do it is to reallocate the array to the new
size and copy the old arrays contents into the new one. Saying that I

would
use the ArrayList approach anyway in a situation where I don't know the
number of elements in advance. If you have then finished the operation of adding items to it then you can *convert* the ArrayList into a strongly

type
array like so:

AType items[] =(AType[]) myArrayList.ToArray( typeof( AType ) );

Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl...
Use an ArrayList. Arrays always are fixed - size in C#

GP

"reb" <gb***@yahoo.com> wrote in message
news:0b****************************@phx.gbl...
> Hi,
>
> How do i increase the size of the array without losing the
> data of that array in c#?
>
> thanks



Nov 15 '05 #5
Of caurse, what means small? But image following code:

class MyClass{
string[] lines;

public MyClass(Stream content){

StreamReader sr = new StreamReader(content);

for(string line=sr.ReadLine(); line != null; line=sr.ReadLine())
AddLine(line);

sr.Close();
}

public void AddLine(string line){
stirng[] copy = new string[lines.lenght];
lines.CopyTo(copy,0);
copy[lines.lenght] = line;
lines = copy;
}
}

If you would read a file line-by-line into this class, it would not perform
good. In this case it would be better to use an ArrayList, but here it would
be ok:

class MyClass2{
string[] lines;

public MyClass2(Stream content){
ArrayList al = new ArrayList();
StreamReader sr = new StreamReader(content);

for(string line=sr.ReadLine(); line != null; line=sr.ReadLine())
al.Add(line);

sr.Close();

lines = (string[])(al.ToArray(typeof(string)));
}
}

It will be executed only once.
GP

"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:uE**************@TK2MSFTNGP10.phx.gbl...
I guess the trouble is though what is the definition of small? I would err
on the side of type safety and only if a performance was shown to be a
problem would I consider an ArrayList being exposed. I know what you mean
though :-)

Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:uh****************@TK2MSFTNGP10.phx.gbl...
If you do this with small arrays, it's ok (I also do this in that case),

but
if the Array rows image that the elements must be copied on each call. In
this case it would be better to use an ArrayList (or an own, strong typed, collectionclass)

GP

"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Just to add the other way to do it is to reallocate the array to the new size and copy the old arrays contents into the new one. Saying that I

would
use the ArrayList approach anyway in a situation where I don't know the number of elements in advance. If you have then finished the operation of adding items to it then you can *convert* the ArrayList into a

strongly type
array like so:

AType items[] =(AType[]) myArrayList.ToArray( typeof( AType ) );

Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl...
> Use an ArrayList. Arrays always are fixed - size in C#
>
> GP
>
> "reb" <gb***@yahoo.com> wrote in message
> news:0b****************************@phx.gbl...
> > Hi,
> >
> > How do i increase the size of the array without losing the
> > data of that array in c#?
> >
> > thanks
>
>



Nov 15 '05 #6
I totally agree with you that's why I said in my original post:

"Saying that I would use the ArrayList approach anyway in a situation where
I don't know the
number of elements in advance"

Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:eE**************@TK2MSFTNGP12.phx.gbl...
Of caurse, what means small? But image following code:

class MyClass{
string[] lines;

public MyClass(Stream content){

StreamReader sr = new StreamReader(content);

for(string line=sr.ReadLine(); line != null; line=sr.ReadLine())
AddLine(line);

sr.Close();
}

public void AddLine(string line){
stirng[] copy = new string[lines.lenght];
lines.CopyTo(copy,0);
copy[lines.lenght] = line;
lines = copy;
}
}

If you would read a file line-by-line into this class, it would not perform good. In this case it would be better to use an ArrayList, but here it would be ok:

class MyClass2{
string[] lines;

public MyClass2(Stream content){
ArrayList al = new ArrayList();
StreamReader sr = new StreamReader(content);

for(string line=sr.ReadLine(); line != null; line=sr.ReadLine())
al.Add(line);

sr.Close();

lines = (string[])(al.ToArray(typeof(string)));
}
}

It will be executed only once.
GP

"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:uE**************@TK2MSFTNGP10.phx.gbl...
I guess the trouble is though what is the definition of small? I would err
on the side of type safety and only if a performance was shown to be a
problem would I consider an ArrayList being exposed. I know what you mean though :-)

Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:uh****************@TK2MSFTNGP10.phx.gbl...
If you do this with small arrays, it's ok (I also do this in that case),
but
if the Array rows image that the elements must be copied on each call. In this case it would be better to use an ArrayList (or an own, strong typed, collectionclass)

GP

"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
> Just to add the other way to do it is to reallocate the array to the new > size and copy the old arrays contents into the new one. Saying that
I would
> use the ArrayList approach anyway in a situation where I don't know

the > number of elements in advance. If you have then finished the
operation of
> adding items to it then you can *convert* the ArrayList into a

strongly type
> array like so:
>
> AType items[] =(AType[]) myArrayList.ToArray( typeof( AType ) );
>
> Regards
> Lee
> "Günter Prossliner" <g.**********@gmx.at> wrote in message
> news:OO**************@TK2MSFTNGP12.phx.gbl...
> > Use an ArrayList. Arrays always are fixed - size in C#
> >
> > GP
> >
> > "reb" <gb***@yahoo.com> wrote in message
> > news:0b****************************@phx.gbl...
> > > Hi,
> > >
> > > How do i increase the size of the array without losing the
> > > data of that array in c#?
> > >
> > > thanks
> >
> >
>
>



Nov 15 '05 #7

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

Similar topics

12
7850
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
8
2883
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
14
8483
by: Sameer | last post by:
Hello, i wish to read a file of int and store into an array dynamically... the size of memory allocated finally, should just be sufficeient to store n integers. I do not know the number of...
8
10583
by: Tee | last post by:
Hi, How do we increase the size of array on runtime and preserve the previous data? I don't want to use ArrayList because the array could be a multi-dimension array. Thanks, Tee
1
7253
by: Mohan | last post by:
Hi, In my application, I need to increase the 2-dimensional array size dynamically. Please let me know the procedure to declare a dynamic 2-dimensional array like Single dimensional array,...
2
3478
by: yoshitha | last post by:
Hi Can any one tell me how to increase size of an array in C#.net with ASP.Net? its very urgent for me thanks yoshitha
18
1929
by: HYRY | last post by:
I want to join two mono wave file to a stereo wave file by only using the default python module. Here is my program, but it is much slower than the C version, so how can I increase the speed? I...
1
2055
by: Amit Sharma | last post by:
hi frndzzzzzz i want to know how we can increase the size of an Array. during execution of the program.
4
2002
by: =?Utf-8?B?R2F1cmF2?= | last post by:
Hello... I posted this a couple of weeks back, but could't get a reply, so trying again! I use the Setup project in C# to create a .msi file to be distributed to the clients. The size of...
0
7094
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
6964
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
7173
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...
0
7305
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...
0
4559
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
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
0
259
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.