Array conversion question

P

Pavils Jurjans

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
 
C

Christiaan van Bergen

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
 
S

Scott Coonce

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 said:
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 said:
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
 
C

Christiaan van Bergen

That's it Scott,

Even better :)

Christiaan

Scott Coonce said:
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 said:
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 said:
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
 
R

Richard Blewett [DevelopMentor]

Christiaan van Bergen said:
That's it Scott,

Even better :)

Christiaan

Scott Coonce said:
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 said:
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" <[email protected]> schreef in bericht
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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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

Pavils Jurjans

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top