locked
DirectCast error part2 RRS feed

  • Question

  • previous post http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2851840&SiteID=1

     

    I did a little more "debugging" and using TypeName I am able to determine that the Object being returned is indeed a Single because a Debug.Print (TypeName(oData(i))) comes out Single in the Immediate Window.

     

    Code Snippet

    Dim oData as Object

     

     

    ReDim oData(51)

    oData = meas.GetData(naRawDAta, naDataFormat_LogMag)

     

    for i = 0 to 50

    Debug.Print(TypeName(oData(i)))

    next i

     

     

     

    So, why am I getting Unable to cast object of type System.Object[] to System.Single[]?

     

     

    Monday, February 18, 2008 3:11 PM

Answers

  • After several emails back and forth, I was able to locate someone at the company that provides the COM DLL who was able to help me out.

     

    The solution turned out to be

     

    Code Snippet

    Dim values() as Single

    Dim oData as Object

    Dim arrayData as Array

    ...

     

    oData = meas.GetData(naRawDAta, naDataFormat_LogMag)

    arrayData = DirectCast(oData, Array)

     

    value(i) = DirectCast(arrayData.GetValue(i), Single)

     

     

     

     

    Tuesday, February 19, 2008 8:08 PM

All replies

  •  

    Redim arrayData(100)

     

    arrayData = DirectCast(meas.getData(AgilentPNA835x.NADataStore.naCorrectedData, AgilentPNA835x.NADataFormat.naDataFormat_SWR), Single())

     

     

    Oh! I wonder if you can do this unless the called code is filling the array itteratively.

     

    For example, this is valid

     

    Dim B() as byte = {1,2,3}

     

    However this will not work

     

    Dim B(2)  as byte = {1,2,3}

     

    You've already dimensioned the array with the REDIM.

    Monday, February 18, 2008 3:32 PM
  • It's just occurred to me, is this a COM DLL that you are calling?

    Monday, February 18, 2008 4:15 PM
  • Yes, it is a COM DLL.

     

    After some experimentation, it seems as if that I will need to leave Option Strict Off for this particular part of the code.

     

    Monday, February 18, 2008 8:39 PM
  •  

    I wonder if that's the problem. Are you sure for .Net that you are not going to have a use Interop and marshaling with this?

     

    The error message may or may not be accurate. Memory contents may be getting terribly confused. The other thing that's true is that there is no assurance that the recipient array is going to be at the same address upon the return by the caller as it was at the beginning of the call. Inside of a .net process, memory used for data storage is not static. The data is being moved frequently by garbage collection. The less available resources a system has, the more garbage collection runs.

    Monday, February 18, 2008 8:51 PM
  • After several emails back and forth, I was able to locate someone at the company that provides the COM DLL who was able to help me out.

     

    The solution turned out to be

     

    Code Snippet

    Dim values() as Single

    Dim oData as Object

    Dim arrayData as Array

    ...

     

    oData = meas.GetData(naRawDAta, naDataFormat_LogMag)

    arrayData = DirectCast(oData, Array)

     

    value(i) = DirectCast(arrayData.GetValue(i), Single)

     

     

     

     

    Tuesday, February 19, 2008 8:08 PM