473,387 Members | 1,530 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.

Array conversion question

Hello,

I think this is classical problem, but I can't figure out how to do it in
most right way (I know ineffective way though ;)

So, I have this method that does some database work and has to return an
array of custom type, let's say

public MyType[] MyMethod(params)
{
ArrayList output = new ArrayList();
}

Since the return array can be of different sizes, dependinf on parameters, I
use standard ArrayList within the body of MyMethod to gather the results.
Whenever a new element is added, something like this is executed:

MyType element = new MyType();
// Some element data populating code
output.Add(element);

Now, when I need to return the output, I need to do some kind of chemistry
so that it is returned as correct MyType[]. What is the most appropriate
way?

Thanks,

Pavils Jurjans
Nov 17 '05 #1
6 1191
Hi Pavils,

A bit crude, I must admit.......(this is probably your ineffective way)

MyType[] Result = new MyType[output.Count];
for(int x=0;x<output.Count;x++)
{
Result[x] = output[x] as MyType;
}

Cheers
Christiaan

"Pavils Jurjans" <pa****@mailbox.riga.lv> schreef in bericht
news:O5**************@TK2MSFTNGP15.phx.gbl...
Hello,

I think this is classical problem, but I can't figure out how to do it in
most right way (I know ineffective way though ;)

So, I have this method that does some database work and has to return an
array of custom type, let's say

public MyType[] MyMethod(params)
{
ArrayList output = new ArrayList();
}

Since the return array can be of different sizes, dependinf on parameters,
I use standard ArrayList within the body of MyMethod to gather the
results. Whenever a new element is added, something like this is executed:

MyType element = new MyType();
// Some element data populating code
output.Add(element);

Now, when I need to return the output, I need to do some kind of chemistry
so that it is returned as correct MyType[]. What is the most appropriate
way?

Thanks,

Pavils Jurjans

Nov 17 '05 #2
Try the following (or something like it, untested code follows):
MyType[] buf = new MyType[somearraylist.Count];
somearraylist.CopyTo(buf);

I think does it.

Scott
"Christiaan van Bergen" <cv********@bergler.nl> wrote in message
news:di**********@azure.qinip.net...
Hi Pavils,

A bit crude, I must admit.......(this is probably your ineffective way)

MyType[] Result = new MyType[output.Count];
for(int x=0;x<output.Count;x++)
{
Result[x] = output[x] as MyType;
}

Cheers
Christiaan

"Pavils Jurjans" <pa****@mailbox.riga.lv> schreef in bericht
news:O5**************@TK2MSFTNGP15.phx.gbl...
Hello,

I think this is classical problem, but I can't figure out how to do it in
most right way (I know ineffective way though ;)

So, I have this method that does some database work and has to return an
array of custom type, let's say

public MyType[] MyMethod(params)
{
ArrayList output = new ArrayList();
}

Since the return array can be of different sizes, dependinf on
parameters, I use standard ArrayList within the body of MyMethod to
gather the results. Whenever a new element is added, something like this
is executed:

MyType element = new MyType();
// Some element data populating code
output.Add(element);

Now, when I need to return the output, I need to do some kind of
chemistry so that it is returned as correct MyType[]. What is the most
appropriate way?

Thanks,

Pavils Jurjans


Nov 17 '05 #3
That's it Scott,

Even better :-)

Christiaan

"Scott Coonce" <sd******@gmail.HEY_YOU.com> schreef in bericht
news:u9**************@TK2MSFTNGP12.phx.gbl...
Try the following (or something like it, untested code follows):
MyType[] buf = new MyType[somearraylist.Count];
somearraylist.CopyTo(buf);

I think does it.

Scott
"Christiaan van Bergen" <cv********@bergler.nl> wrote in message
news:di**********@azure.qinip.net...
Hi Pavils,

A bit crude, I must admit.......(this is probably your ineffective way)

MyType[] Result = new MyType[output.Count];
for(int x=0;x<output.Count;x++)
{
Result[x] = output[x] as MyType;
}

Cheers
Christiaan

"Pavils Jurjans" <pa****@mailbox.riga.lv> schreef in bericht
news:O5**************@TK2MSFTNGP15.phx.gbl...
Hello,

I think this is classical problem, but I can't figure out how to do it
in most right way (I know ineffective way though ;)

So, I have this method that does some database work and has to return an
array of custom type, let's say

public MyType[] MyMethod(params)
{
ArrayList output = new ArrayList();
}

Since the return array can be of different sizes, dependinf on
parameters, I use standard ArrayList within the body of MyMethod to
gather the results. Whenever a new element is added, something like this
is executed:

MyType element = new MyType();
// Some element data populating code
output.Add(element);

Now, when I need to return the output, I need to do some kind of
chemistry so that it is returned as correct MyType[]. What is the most
appropriate way?

Thanks,

Pavils Jurjans



Nov 17 '05 #4
"Christiaan van Bergen" <cv********@bergler.nl> wrote in message
news:di**********@azure.qinip.net...
That's it Scott,

Even better :-)

Christiaan

"Scott Coonce" <sd******@gmail.HEY_YOU.com> schreef in bericht
news:u9**************@TK2MSFTNGP12.phx.gbl...
Try the following (or something like it, untested code follows):
MyType[] buf = new MyType[somearraylist.Count];
somearraylist.CopyTo(buf);

I think does it.

Scott
"Christiaan van Bergen" <cv********@bergler.nl> wrote in message
news:di**********@azure.qinip.net...
Hi Pavils,

A bit crude, I must admit.......(this is probably your ineffective way)

MyType[] Result = new MyType[output.Count];
for(int x=0;x<output.Count;x++)
{
Result[x] = output[x] as MyType;
}

Cheers
Christiaan

"Pavils Jurjans" <pa****@mailbox.riga.lv> schreef in bericht
news:O5**************@TK2MSFTNGP15.phx.gbl...
Hello,

I think this is classical problem, but I can't figure out how to do it
in most right way (I know ineffective way though ;)

So, I have this method that does some database work and has to return
an array of custom type, let's say

public MyType[] MyMethod(params)
{
ArrayList output = new ArrayList();
}

Since the return array can be of different sizes, dependinf on
parameters, I use standard ArrayList within the body of MyMethod to
gather the results. Whenever a new element is added, something like
this is executed:

MyType element = new MyType();
// Some element data populating code
output.Add(element);

Now, when I need to return the output, I need to do some kind of
chemistry so that it is returned as correct MyType[]. What is the most
appropriate way?

Thanks,

Pavils Jurjans


static int[] Foo()
{
ArrayList arr = new ArrayList();
...
return (int[])arr.ToArray(typeof(int));
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #5
Hi,
In addition to the others proposals a possible solution would be (assuming
you are querying a DB with an SP and returning a DataReader):

create a output parameter, will contain the number of rows.
execute the reader
create the correct size array
populate the array

return the array
The only thing pending would be how to get the number of rows in the SP,
that depends of your DB and your query.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Pavils Jurjans" <pa****@mailbox.riga.lv> wrote in message
news:O5**************@TK2MSFTNGP15.phx.gbl...
Hello,

I think this is classical problem, but I can't figure out how to do it in
most right way (I know ineffective way though ;)

So, I have this method that does some database work and has to return an
array of custom type, let's say

public MyType[] MyMethod(params)
{
ArrayList output = new ArrayList();
}

Since the return array can be of different sizes, dependinf on parameters,
I use standard ArrayList within the body of MyMethod to gather the
results. Whenever a new element is added, something like this is executed:

MyType element = new MyType();
// Some element data populating code
output.Add(element);

Now, when I need to return the output, I need to do some kind of chemistry
so that it is returned as correct MyType[]. What is the most appropriate
way?

Thanks,

Pavils Jurjans

Nov 17 '05 #6
>
static int[] Foo()
{
ArrayList arr = new ArrayList();
...
return (int[])arr.ToArray(typeof(int));
}

Richard Blewett - DevelopMentor


Thanks, this proved to be the shortest and best!

Rgds, Pavils
Nov 17 '05 #7

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

Similar topics

11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
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$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.