|
-
Apr 7th, 2006, 05:17 AM
#1
Thread Starter
Junior Member
convert from 'ref double[]' to 'ref object'
I developed a c++ ATL com dll. I'm trying to use it in C#.
In C# I have a method with a parametr of "ref object" type (in C++ is a VARIANT* I convert it to SAFEARRAY of doubles).
For use this method I create a double[] variable but it throws me this error:
Argument '3': cannot convert from 'ref double[]' to 'ref object'
If I create
object x =null;
And after call to method I execute: double[] myarray = (double []) x;
It run ok, but I would like avoid those steps.
Thanksss
_________________________
Hip Hop Directo
De Chiste
-
Apr 7th, 2006, 05:40 AM
#2
Re: convert from 'ref double[]' to 'ref object'
The problem is that you are passing the array by reference as a more general type. If it was by value it wouldn't be an issue but you can't change the type like that with a parameter passed by reference. The problem is because within the method the argument is an object reference so you could assign any type of object to it. It's supposed to be a double[] when the method completes though, so that is obviously a problem. Take a look at this example:
Code:
private void Form1_Load(object sender, EventArgs e)
{
Double[] arr = new Double[10];
// The parameter would go in as a Double[]...
this.PassByRef(ref arr);
}
private void PassByRef(ref object arr)
{
// ...and be changed to a string here. That can't be allowed to happen.
arr = "Hello World";
}
The compiler will not let you do that as it cannot guarantee type safety. You can change the type of a parameter passed by reference to a more specific type with a cast, but not to a less specific type.
Last edited by jmcilhinney; Apr 7th, 2006 at 05:50 AM.
Reason: Added "with a cast" to last sentence.
-
Apr 7th, 2006, 06:33 AM
#3
Thread Starter
Junior Member
Re: convert from 'ref double[]' to 'ref object'
Thanks.
Then I only can do the conversion after call the method?
-
Apr 7th, 2006, 06:40 AM
#4
Re: convert from 'ref double[]' to 'ref object'
 Originally Posted by wakeup
Thanks.
Then I only can do the conversion after call the method?
I'm afraid so, although it is the only way it could be. If you were allowed to do what you wanted you could end up run time exceptions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|