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

VB Equivalent to C++ Casts

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


This C++ 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 C++ integer casts always truncate, while the VB integer conversion operators always round.  For this reason, in order to achieve the same results as the C++ casts, a call to the .NET Math.Truncate function is required prior to the call to the CInt, CLng, and CShort operators when converting C++ integer casts.


This C++ code:
i = (int)someDouble;

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

Use C++ to VB Converter to convert from C++ to VB.     

Additional resource: VB.NET and C++ Equivalents

Copyright © 2004 – 2021 Tangible Software Solutions, Inc.