• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 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:

e.g.,

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 macro and the related shortcut VB macros (CInt, CBool, etc.).

A further complication is that Java integer casts always truncate, while the VB integer conversion macros 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 macros when converting Java integer casts.

e.g.,

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 © 1997 – 2017 Tangible Software Solutions Inc.