convert string to ref System.Array

  • Thread starter Thread starter Janus Knudsen
  • Start date Start date
J

Janus Knudsen

Hello

Im getting an error when I try to convert like this:
string[] arrDSN;

objDSN.GetDataSourceList(arrDSN)

And the error: Argument '1': cannot convert from 'string[]' to 'ref
System.Array'

Kind regards
 
...
Im getting an error when I try to convert like this:
string[] arrDSN;

objDSN.GetDataSourceList(arrDSN)

And the error: Argument '1': cannot
convert from 'string[]' to 'ref
System.Array'

Two things to do.

Cast the string array to a System.Array:

Array sa = (Array) arrDSN;

The error message tells us that the method expects a "reference":

objDSN.GetDataSourceList(ref sa)

// Bjorn A
 
A ref param must be an lvalue variable of exactly the expected type, eg it
can't be a variable of a derived class. In your case, if you created a new
variable of type System.Array you could pass a ref to that.

Brad Williams
 
Thank you very much!

However if I dont assign the arrDSN like this:
string[] arrDSN = new string[10];

I also get an error, is there any way to avoid this? - Maybe have the array
to autoexpand or to use it unassigned... It cannot be castet to an array:
Array sa = (Array)arrDSN; - when it's unassigned apparently?


Kind regards
Janus


Bjorn Abelli said:
...
Im getting an error when I try to convert like this:
string[] arrDSN;

objDSN.GetDataSourceList(arrDSN)

And the error: Argument '1': cannot
convert from 'string[]' to 'ref
System.Array'

Two things to do.

Cast the string array to a System.Array:

Array sa = (Array) arrDSN;

The error message tells us that the method expects a "reference":

objDSN.GetDataSourceList(ref sa)

// Bjorn A
 
...
However if I dont assign the arrDSN like this:
string[] arrDSN = new string[10];

I also get an error, is there any way to avoid this?

No, not completely, as you can't "use" an unassigned variable.

But as the GetDataSourceList suggests that it actually is going to *change*
the reference to your array anyway as it's using the ref keyword, I believe
you can use an "empty" array.

string[] arrDSN = new string[0];


// Bjorn A
 

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

Back
Top