What is the cast expression equivalent of VB.NET's CType in Visual Basic 6?
Ctype() I believe. The C* (CDate(), CStr(), etc) are holdovers for the most part.
There are a number of them depending on the type you are casting to
cint() Cast to integer
cstr() cast to string
clng() cast to long
cdbl() cast to double
cdate() cast to date
It also has implicit casting so you can do this myString=myInt
The casts already mentioned are correct, but if the type is an Object then you have to use "Set" in VB6, such as:
If IsObject(myObject) Then
Set myObject = Value ' VB6 does not have CType(Value, MyObjectType)
Else
myObject = Value ' VB6 does not have CType(Value, MyObjectType)
End If
That, of course, depends on the type you are casting to. Almost all user classes are objects as well as Collection, Dictionary, and many others. The built-in types such as long, integer, boolean, etc. are obviously not objects.
Let's say you have an object of ChildClass (child) that you want to cast to BaseClass. You do this:
Dim base As BaseClass
Set base = child
Because of the way VB6 handles compile-time type safety, you can just do that without any extra syntax.
Note: Given that everyone else seems to have mentioned CType
, I may just have misunderstood the question completely, and I apologise if that's the case!
Quite a few posters seem to have misread the question, so I will try to set things straight by rephrasing the question and summarizing the correct answers given so far.
Problem
I want to cast data of one type to another type. In my VB.NET code I would use
CType
to do this. However, when I try to useCType
in VB6, I get a "Sub or Function not defined" error. So, how can I perform casts in VB6 ifCType
won't work?
Solution
As you may have discovered, VB6 does not have a CType
function like VB.NET does. However, the other conversion functions (those that have names beginning with C
), which you may have encountered in VB.NET code, such as CInt and CStr, do exist in VB6, and you can use them to convert to and from non-object types. There is no built-in function for converting an object of one class to an object of another class. Keep in mind that VB6, unlike VB.NET, does not support inheritance. A class in VB6 can implement one or more interfaces, but it cannot inherit from another class. However, if a object's class implements more than one interface, you can use the Set
statement to cast an object to one of the interfaces it supports (as Ant suggested). An extended version of Ant's code example is provided below:
Example: Casting a class to one of its supported interfaces
Dim base As BaseClass
Dim child As ChildClass 'implements BaseClass'
Set child = New ChildClass
Set base = child '"Cast" child to BaseClass'
Built-in type conversion functions in VB6
Below is a complete list of the built-in conversion functions available in VB6, taken directly from the VB6 Help file.
CBool
Returns
Boolean
Description
Convert expression to Boolean.
Range for expression argument:
Any valid string or numeric expression.
CByte
Returns
Byte
Description
Convert expression to Byte.
Range for expression argument:
0 to 255.
CCur
Returns
Currency
Description
Convert expression to Currency.
Range for expression argument:
-922,337,203,685,477.5808 to 922,337,203,685,477.5807.
CDate
Returns
Date
Description
Convert expression to Date.
Range for expression argument:
Any valid date expression.
CDbl
Returns
Double
Description
Convert expression to Double.
Range for expression argument:
-1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
CDec
Returns
Decimal
Description
Convert expression to Decimal.
Range for expression argument:
+/-79,228,162,514,264,337,593,543,950,335 for zero-scaled numbers, that is, numbers with no decimal places. For numbers with 28 decimal places, the range is +/-7.9228162514264337593543950335. The smallest possible non-zero number is 0.0000000000000000000000000001.
CInt
Returns
Integer
Description
Convert expression to Long.
Range for expression argument:
-32,768 to 32,767; fractions are rounded.
CLng
Returns
Long
Description
Convert expression to Long.
Range for expression argument:
-2,147,483,648 to 2,147,483,647; fractions are rounded.
CSng
Returns
Single
Description
Convert expression to Single.
Range for expression argument:
-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
CStr
Returns
String
Description
Convert expression to String.
Range for expression argument:
Returns for CStr depend on the expression argument.
CVar
Returns
Variant
Description
Convert expression to Variant.
Range for expression argument:
Same range as Double for numerics. Same range as String for non-numerics.
Conversions are not "casts" at all. For example try:
MsgBox CLng(CBool(3&))
The result is -1, not 3. This is because those are conversion functions, not casts. Language is important!