开发者

Cannot Convert ref double[] to ref object

It seems like I'm on here asking a question just about every day now. I'm not sure if that's a good thing or a bad thing...

Today's "Flavor-Of-The-WTF" involves my complete and utter cluelessness when using a function from a NI Measurement Studio object. As with most of my previous questions, this is in regards to an internship project where I've been tasked with translating a VB6 project into C# in the .Net Framework 2.0.

The original VB code looks something like this:

Public Sub FFT(ZData() As Single, FFTData() As Single)
    Dim realdata As Variant
    Dim imgdata As Variant

    // (... Some unrelated other code in here ...)

    // Here we pass three variables to a NI CWDSP object's ReFFT function
    CWDSP1.ReFFT ZData, realdata, imgdata

    // (... More unrelated code ...)
End Sub

I stuck in a MsgBox at one point to see how realdata and imgdata were being interpreted. They're both Double()s. It works just fine for what it does, in the original program. Admittedly, my intimate knowledge of the original program is only mediocre, as I picked up the project recently, and only have a few years of programming under my belt (and no years of mechanical engineering. The programmer was a mechanical engineer, the application itself a measuring tool for machine output).

Taking the code over to C#, I tried rewriting it as such:

private void FFT(float[] ZData, float[] FFTData){
    double[] realData = new double[1000];
    double[] imgData = new double[1000];

    // (... Unrelated intermediate code ...)

    DSP.ReFFT(ZData, realData, imgData);

    // (... Unrelated intermediate code ...)
}

As you can see, I started by doing it basically the same way as the original VB. The following error came up: Cannot Convert double[] to ref object

Well, that's no good. So I tried: DSP.ReFFT(ZData, ref realData, ref imgData);

Only to get back: Cannot Convert ref double[] to ref object

So I did what I thought was obvious. I boxed realData and imgData into objects, and passed them to the function. It's not too fond of that, however. If I pass the new objects without ref, it insists that I need to pass them with ref. If I pass them with ref, it gives me the error:

Cannot Convert ref double[] to ref object.

Huh... that looks familiar. I finally get the compiler to stop producing errors when I change double[] to Object in the instantiation of the two variables. But... when I run the function, I get a Type Mismatch error.

I've really got no idea where I go from here. There's little to no information about programming for C# with Measurement Studio out there on the internet. Even if there was, I'm sure the solution is much more simpl开发者_如何学编程e than I expect it to be. History has taught me that when there's a bug I can't figure out, it's almost always something stupid.


Give this a go in your FFT function:

object realData = new double[1000];
object imgData = new double[1000];
ReFFT(new object(), ref realData, ref imgData);

double[] realDataArray = (double[])realData;

by instantiating the object as a new double[1000], you are establishing that although you are treating realData as an object, its' underlying type is double[]. Later, after the method call, you're closing the realData array by creating a new one and casting the previous one to the original type. It may be more boxing/unboxing than is strictly needed, but hopefully it gets the point across

Try 2:

double[] somerealData = new double[1000];
double[] someimgData = new double[1000];
object realData, imgData;
realData = somerealData;
imgData = someimgData;

ReFFT(new object(), ref realData, ref imgData);

double[] realDataArray = (double[])realData;
Console.WriteLine(realDataArray.Length);
realDataArray[0] = 1.0d;
Console.WriteLine(realDataArray[0]);

I've tried the second approach in a .NET 2.0 console app, so I'm pretty sure that it works. If it doesn't, then we'll need to take a step back and re-assess the situation.

Try 3: Just noticed that the first parameter of the ReFFT method is a float[]. Let's try this:

double[] somerealData = new double[1000];
double[] someimgData = new double[1000];
//float[] zData placeholder since array comes from input param

object realData, imgData, zObj;
realData = somerealData;
imgData = someimgData;
zObj = zData;

ReFFT(zObj, ref realData, ref imgData);

double[] realDataArray = (double[])realData;
Console.WriteLine(realDataArray.Length);
realDataArray[0] = 1.0d;
Console.WriteLine(realDataArray[0]);


What about just trying to send it an object, then check the type of what you get back. So something like this:

    private void FFT(float[] ZData, float[] FFTData)
    {
        object realData = new object();
        object imgData = new object();
    
        // (... Unrelated intermediate code ...)
    
        DSP.ReFFT(ZData, ref realData, ref imgData);
        
        // Find out what the type actual is (this will probably only work
        // if the values are assigned with the new data type)
        // but worth a shot:
        type t1 = typeof(realData);
        type t2 = typeof(imgData);
    
        // if t1 and t2 show the actual types, replace the
        // type above (new object() with the type here)
    
        // (... Unrelated intermediate code ...)
    }

Update: Just saw your last comment above. You'll probably want to create three seperate objects. Test1, Test2 and Test3 all as new object()'s.

From this post, it says for documentation to do the following:

Start >> All Programs >> National Instruments >> Measurement Studio 7.1 for VS .NET 2003 >> Measurement Studio Documentation

Then select "Measurement Studio" under the Filter pull down.

Now navigate the help tree to "NI Measurment Studio Help >> NI Measurement Studio .NET Class Library >> Reference >> National Instruments.Analysis.SpectralMeasurements >> Measurements Class >> Methods >> ReFFT??? Method.


You say that DSP is COM, and the odd errors you're getting suggests to me that the underlying problem is "marshalling" the parameters between .NET and COM. I am no expert on this, so this is more along the lines of a suggestion to investigate how one would encapsulate DSP in a COM-interop layer and calling it through that layer.

Here's a good starting point on MSDN: Introduction to COM Interop


I never did figure out what was wrong with the function, so this answer might not help some people. Nevertheless, if you're using Measurement Studio in C# like I am and need to do a FFT, there was apparently a .NET class that includes it, and appears to work.

NationalInstruments.Analysis.Dsp.Transforms.FFT (and InverseFFT and a bunch of others). I'd still love to know what the deal with the other one was, though...

Thanks everyone for your suggestions. They were all logical ones that probably should have worked, but didn't for some unknown reason (likely relating to the inner workings of the function).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜