• Saves Valuable Time
  • Trusted Accuracy for 14 Years
  • 15-Day Money-Back Guarantee

C++ Equivalent to VB Casts

In native C++, the closest equivalent to the standard VB casting macros (CType and the corresponding CInt, CStr, etc.) is static_cast. The main exception are casts from floating point types to integer types (e.g., CInt(myDouble)) - in these cases, you need a method to perform VB 'banker's rounding' (our VB to C++ Converter inserts a helper class for this).

In C++/CLI, the closest equivalent are calls to the System::Convert methods if the appropriate method exists, otherwise they are converted to the safe_cast operator.

The behavior of all of these methods may be subtly different from the VB casting macros though, so you should always test the behavior of your converted C++ code.

The VB TryCast operator is converted to the C++ dynamic_cast operator.

VB C++
x = CBool(y)
x = CInt(y)
x = CChar(y)
x = CStr(y)

x = CType(myObject, FooType)
x = DirectCast(y, FooType)
x = TryCast(myObject, FooType)
x = static_cast<bool>(y);
x = static_cast<int>(y);
x = static_cast<wchar_t>(y);
x = static_cast<std::wstring>(y);

x = static_cast<FooType*>(myObject);
x = static_cast<FooType*>(myObject);
x = dynamic_cast<FooType*>(myObject);

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

Additional resource: VB.NET and C++ Equivalents

Copyright © 1997 – 2018 Tangible Software Solutions Inc.