Search code examples
c#objectreferencemeasurement-studio

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 simple 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.


Solution

  • 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).