• Saves Valuable Time
  • Trusted Accuracy since 2004
  • 15-Day Money-Back Guarantee

VB Equivalent to Java Casts

The closest VB equivalent to the standard Java casting operator is the DirectCast operator.  However, use of this operator is invalid in many cases where standard Java casting is used:


This Java code works fine:
i = (int)someDouble;

but the following VB code will not compile:
i = DirectCast(someDouble, Integer)

For this reason, the more reliable equivalent in VB is the CType operator and the related shortcut VB operators (CInt, CBool, etc.).

A further complication is that Java integer casts always truncate, while the VB integer conversion operators always round.  For this reason, in order to achieve the same results as the Java casts, a call to the .NET Math.Truncate function is required prior to the call to the CInt, CLng, and CShort operators when converting Java integer casts.


This Java code:
i = (int)someDouble;

has this VB equivalent:
i = CInt(Math.Truncate(someDouble))

Use Java to VB Converter to convert from Java to VB.     

Additional resource: VB.NET and Java Equivalents

Copyright © 2004 – 2021 Tangible Software Solutions, Inc.