Page 1 of 1

TypeCast double to integer for Array( Index )

Posted: Thu May 31, 2012 11:09 pm
by TeraByte
The following causes compiler to hiccup:

Code: Select all

Define d1.d
Dim MyArray.d(10)

d1 = 2.0
MyArray( d1 ) = 123.45
So I changed it to this which works:

Code: Select all

Define d1.d
Dim MyArray.d(10)

d1 = 2.0
MyArray( Int(d1) ) = 123.45
Question:
My snippet is just to demonstrate the issue where my index is being passed in by a 3rd party app as a double. Thus I need to typecast it to an integer for the array.

Is there a more efficient way to typecast from double to integer for the array index?

Re: TypeCast double to integer for Array( Index )

Posted: Fri Jun 01, 2012 4:53 pm
by TeraByte
Is this more efficient?

Code: Select all

Define d1.d,  i1.i
Dim MyArray.d(10)

d1 = 2.0
i1 = d1
MyArray( i1 ) = 123.45

Re: TypeCast double to integer for Array( Index )

Posted: Fri Jun 01, 2012 11:54 pm
by IdeasVacuum
...the most efficient way would surely be to read the passed value as an int instead of as a double - how is the other app passing the value exactly?

Re: TypeCast double to integer for Array( Index )

Posted: Sat Jun 02, 2012 5:35 pm
by TeraByte
The 3rd party app is passing me an array of doubles. The array is multi purpose whereby sometimes the values in the array are to be used as doubles in math calculations, yet other times, as in my case, as an array index.

Re: TypeCast double to integer for Array( Index )

Posted: Sat Jun 02, 2012 8:45 pm
by IdeasVacuum
Yes, but lets say your eyes are closed and nobody has told you what type of data you will receive - it could be bananas, it could be doubles.
- how is the other app passing the value exactly?

Re: TypeCast double to integer for Array( Index )

Posted: Sat Jun 02, 2012 9:16 pm
by skywalk
How are you declaring your 3rd party dll?
Maybe use a prototype and call it differently depending on double array or integer array?

Code: Select all

Prototype[C].i your_dll_fn(param1.i, param2.i, etc.i, *nPoints.i, *IntOrDbl_array)
Global.i hdll = OpenLibrary(#PB_Any, "C:\Your_dll_path\your_dll.dll")
If hdll
  Global Your_dll_fn.Your_dll_fn = GetFunction(hdll, "Your_dll_fn")
Else
  MessageRequester("", "Error opening dll", #MB_ICONERROR)
EndIf
Define.i nPts
Dim arD.d(10)
Dim arI.i(10)
; Call dll with Double array
  your_dll_fn(0, 0, 0, @nPts, arD())
; Or Call it with Integer array
  your_dll_fn(0, 0, 0, @nPts, arI())

If hdll
  CloseLibrary(hdll)
EndIf