Helper methods for data type conversion

Generally, data type conversion between ActiveX (Visual Basic and VBScript) and Java methods occurs automatically, as described in ActiveX to enterprise bean bridge, converting data types. However, for cases where automatic conversion is not possible, the Byte and Currency helper functions are provided.

See the Code example disclaimer for legal information about these code examples.

Byte helper function

Because the Java Byte data type is signed (-127 through 128) and the Visual Basic Byte data type is unsigned (0 through 255), convert unsigned Bytes to a Visual Basic Integers, which look like the Java signed byte. To make this conversion, you can use this helper function:

  Private Function GetIntFromJavaByte(Byte jByte) as Integer
    GetIntFromJavaByte = (CInt(jByte) + 128) Mod 256 - 128
  End Function

Currency helper function

Visual Basic 6.0 cannot properly handle 64-bit integers like Java methods can (as the Long data type). Therefore, Visual Basic uses the Currency type, which is intrinsically a 64-bit data type. The only problem with using the Currency type (the Variant type VT_CY) is that a decimal point is inserted into the type. To extract and manipulate the 64-bit Long value in Visual Basic, use code like the following example. For more details on this technique for converting Currency data types, see Q189862 HOWTO: Do 64-bit Arithmetic in VBA in the Microsoft Knowledge Base. Link outside Information Center

  ' Currency Helper Types
  Private Type MungeCurr
    Value As Currency
  End Type
  Private Type Munge2Long
    LoValue As Long
    HiValue As Long
  End Type

  ' Currency Helper Functions
  Private Function CurrToText(ByVal Value As Currency) As String
    Dim Temp As String, L As Long
    Temp = Format$(Value, "#.0000")
    L = Len(Temp)
    Temp = Left$(Temp, L - 5) & Right$(Temp, 4)
    Do While Len(Temp) > 1 And Left$(Temp, 1) = "0"
      Temp = Mid$(Temp, 2)
    Loop
    Do While Len(Temp) > 2 And Left$(Temp, 2) = "-0"
      Temp = "-" & Mid$(Temp, 3)
    Loop
    CurrToText = Temp
  End Function

  Private Function TextToCurr(ByVal Value As String) As Currency
    Dim L As Long, Negative As Boolean
    Value = Trim$(Value)
    If Left$(Value, 1) = "-" Then
      Negative = True
      Value = Mid$(Value, 2)
    End If
      L = Len(Value)
      If L < 4 Then
        TextToCurr = CCur(IIf(Negative, "-0.", "0.") & _
                          Right$("0000" & Value, 4))
    Else
      TextToCurr = CCur(IIf(Negative, "-", "") & _
                        Left$(Value, L - 4) & "." & Right$(Value, 4))
    End If
  End Function

  ' Java Long as Currency Usage Example
  Dim LC As MungeCurr
  Dim L2 As Munge2Long

  ' Assign a Currency Value (really a Java Long)
    to the MungeCurr type variable
  LC.Value = cyTestIn

  ' Coerce the value to the Munge2Long type variable
  LSet L2 = LC

  ' Perform some operation on the value, now that we
  ' have it available in two 32-bit chunks
  L2.LoValue = L2.LoValue + 1

  ' Coerce the Munge value back into a currency value
  LSet LC = L2
  cyTestIn = LC.Value