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

Dynamic arrays

How can i achieve the dynamic arrays concept in C#?
I have used the ArrayLists of C# but in few cases it is
failing like when i pass these dynamic arrays a arguments
to functions b'coz in those functions directly i cant
assign the arraylist values to other variables (as they
are objects)

How can i do that?

In unmanaged code it is like this which i want to do it in
c#

My requirement is there is an dynamic array whose values
are assigned based on the requirement like
if an array name array1

it is declared as
int array1[]

array1[0]=2
array1[1]=4
array1[10]=5

then now the size of array is 10
0th value is 2
1st value is 4
2nd to 8th values are assigned as 0's
and 9 th vales is 5

some where in the code i will call a function f1 to which
i will pass the argument the array1
f1(array1[])

How can i achieve this requirement in C#?

Thank you
Vannela
Jul 21 '05 #1
4 1563
If I'm in a scenario like this, I use an ArrayList to build my array, and
when I need to pass it as a parameter (e.g. a "real" array), I use the
ToArray function.

myArrayList.ToArray(typeof(MyType[]))

Jan

"Vannela" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
How can i achieve the dynamic arrays concept in C#?
I have used the ArrayLists of C# but in few cases it is
failing like when i pass these dynamic arrays a arguments
to functions b'coz in those functions directly i cant
assign the arraylist values to other variables (as they
are objects)

How can i do that?

In unmanaged code it is like this which i want to do it in
c#

My requirement is there is an dynamic array whose values
are assigned based on the requirement like
if an array name array1

it is declared as
int array1[]

array1[0]=2
array1[1]=4
array1[10]=5

then now the size of array is 10
0th value is 2
1st value is 4
2nd to 8th values are assigned as 0's
and 9 th vales is 5

some where in the code i will call a function f1 to which
i will pass the argument the array1
f1(array1[])

How can i achieve this requirement in C#?

Thank you
Vannela

Jul 21 '05 #2
I used this code but giving error
ArrayList a=new ArrayList();
a.Add(1);
a.Add(2);
int[] aa=(int[])a.ToArray(new Int64().GetType());

Error: Atleast one element in the source array could not
be cast to destinationtype array
-----Original Message-----
If I'm in a scenario like this, I use an ArrayList to build my array, andwhen I need to pass it as a parameter (e.g. a "real" array), I use theToArray function.

myArrayList.ToArray(typeof(MyType[]))

Jan

"Vannela" <an*******@discussions.microsoft.com> wrote in messagenews:01****************************@phx.gbl...
How can i achieve the dynamic arrays concept in C#?
I have used the ArrayLists of C# but in few cases it is
failing like when i pass these dynamic arrays a arguments to functions b'coz in those functions directly i cant
assign the arraylist values to other variables (as they
are objects)

How can i do that?

In unmanaged code it is like this which i want to do it in c#

My requirement is there is an dynamic array whose values
are assigned based on the requirement like
if an array name array1

it is declared as
int array1[]

array1[0]=2
array1[1]=4
array1[10]=5

then now the size of array is 10
0th value is 2
1st value is 4
2nd to 8th values are assigned as 0's
and 9 th vales is 5

some where in the code i will call a function f1 to which i will pass the argument the array1
f1(array1[])

How can i achieve this requirement in C#?

Thank you
Vannela

.

Jul 21 '05 #3
Try this:

ArrayList a=new ArrayList();
a.Add(1);
a.Add(2);
int[] aa=(int[])a.ToArray(typeof(int));
--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
<an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
I used this code but giving error
ArrayList a=new ArrayList();
a.Add(1);
a.Add(2);
int[] aa=(int[])a.ToArray(new Int64().GetType());

Error: Atleast one element in the source array could not
be cast to destinationtype array
-----Original Message-----
If I'm in a scenario like this, I use an ArrayList to

build my array, and
when I need to pass it as a parameter (e.g. a "real"

array), I use the
ToArray function.

myArrayList.ToArray(typeof(MyType[]))

Jan

"Vannela" <an*******@discussions.microsoft.com> wrote in

message
news:01****************************@phx.gbl...
How can i achieve the dynamic arrays concept in C#?
I have used the ArrayLists of C# but in few cases it is
failing like when i pass these dynamic arrays a arguments to functions b'coz in those functions directly i cant
assign the arraylist values to other variables (as they
are objects)

How can i do that?

In unmanaged code it is like this which i want to do it in c#

My requirement is there is an dynamic array whose values
are assigned based on the requirement like
if an array name array1

it is declared as
int array1[]

array1[0]=2
array1[1]=4
array1[10]=5

then now the size of array is 10
0th value is 2
1st value is 4
2nd to 8th values are assigned as 0's
and 9 th vales is 5

some where in the code i will call a function f1 to which i will pass the argument the array1
f1(array1[])

How can i achieve this requirement in C#?

Thank you
Vannela

.

Jul 21 '05 #4
<an*******@discussions.microsoft.com> wrote:
I used this code but giving error
ArrayList a=new ArrayList();
a.Add(1);
a.Add(2);
int[] aa=(int[])a.ToArray(new Int64().GetType());

Error: Atleast one element in the source array could not
be cast to destinationtype array


Yes, that's because you've added Int32s, not Int64. You might also like
to have a look at the typeof operator - you don't need to create a new
instance of a type to get its Type object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

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

Similar topics

3
by: meyousikmann | last post by:
The following code just sets up and fills a dynamic array of integers. #include <cstdlib> int main() { int* intArray = NULL; int count; count = 20;
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
3
by: genc ymeri | last post by:
Hi, What can I use in C# for dynamic arrays ???? I have some records (struts in ..Net) and want to store them in a dynamic "arrays" or object list. I noticed the in C# arrays' length can't be...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
4
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... ...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
1
by: KioKrofov | last post by:
Hello, I am trying to find out how Dynamic Arrays are actually stored in memory. Really, I want to know how Vectors are stored in memory, but I deduce they are stored the same way. If you...
2
by: headware | last post by:
Do dynamic arrays declared using ReDim have to be freed? I assume that if it's an array of dynamically created objects (e.g. Scripting.Dictionary), each one of those objects will have to be set to...
4
by: Sunny | last post by:
Hi, Is there a way in javascript to create Dynamic arrays or arrays on fly. Something Like: var "ptsgN"+sd = new Array(); Here sd is incrementing by 1. I have lots of data that I am...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.