Dynamic arrays

  • Thread starter Thread starter Vannela
  • Start date Start date
V

Vannela

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
 
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
 
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

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


.
 
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


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

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


.
 
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.
 
Back
Top