473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1575
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.ToA rray(typeof(MyT ype[]))

Jan

"Vannela" <an*******@disc ussions.microso ft.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.To Array(typeof(My Type[]))

Jan

"Vannela" <an*******@disc ussions.microso ft.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(type of(int));
--
Greetz

Jan Tielens
_______________ _______________ __
Read my weblog: http://weblogs.asp.net/jan
<an*******@disc ussions.microso ft.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.To Array(typeof(My Type[]))

Jan

"Vannela" <an*******@disc ussions.microso ft.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*******@disc ussions.microso ft.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.co m>
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
4038
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
7667
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 simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it...
3
2806
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 extented in run time. So, what should I use ? Thank You in advance. PS:
60
10112
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: > http://groups.google.com/group/comp.lang.c++/msg/db577c43260a5310?hl > >Another way is to create a one dimensional array and handle the >indexing yourself (index = row * row_size + col). This is...
4
2459
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..... //trying to get input from the user to add all the numbers that user inputs //tried to do it without dynamic memory usage but probably cannot achieve it...
2
7039
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, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created...
4
5055
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 deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each...
1
2666
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 have a dynamic array initialized at size 50, and it then needs to increase to size 75, what happens? Does element 49 contain some sort of pointer to...
2
2104
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 Nothing in a for loop, but what about the array itself? Or what if the array is dynamic but it just consists of integers or strings, not...
4
5414
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 putting in arrays.
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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 we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.