示例:使用数据类型转换的辅助控件方法的 ActiveX 客户机应用程序
通常,自动转换 ActiveX(Visual Basic 和 VBScript)和 Java™ 方法直接的数据类型,这个在 ActiveX to EJB 网桥,转换数据类型中描述的一样。然而,字节帮助器功能和货币帮助器功能是在不可能进行自动转换的情况下提供的:
- 字节帮助器功能因为 Java 字节数据类型有正负之分(-127 到 128)而 Visual Basic 字节数据类型没有正负之分(0 到 255),因此将不带符号的字节转换为 Visual Basic 整数,这与 Java 带符号字节类似。要进行此转换,您可以使用下列帮助器功能:
Private Function GetIntFromJavaByte(Byte jByte) as Integer GetIntFromJavaByte = (CInt(jByte) + 128) Mod 256 - 128 End Function
- 货币帮助器功能Visual Basic 6.0 无法正确地处理 64 位整数,而 Java 方法可以(和长整型数据类型一样)。 因此,Visual Basic 使用货币类型,该数据是内在地 64 位数据类型。使用货币类型的唯一副作用是在类型中插入了小数点。 要在 Visual Basic 中抽取和操纵 64 位长型值,使用与下列示例相似的代码。有关这种转换货币数据类型技术的更多详细信息,请参阅 Microsoft Knowledge Base 中的“Q189862, HOWTO: Do 64-bit Arithmetic in VBA”。
' 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