[ACCEPTED]-VB6 Cast Expression-casting
There are a number of them depending on 2 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 1 casting so you can do this myString=myInt
Quite a few posters seem to have misread 48 the question, so I will try to set things 47 straight by rephrasing the question and 46 summarizing the correct answers given so 45 far.
Problem
I want to cast data of one type to another 44 type. In my VB.NET code I would use
CType
to 43 do this. However, when I try to useCType
in 42 VB6, I get a "Sub or Function not defined" error. So, how 41 can I perform casts in VB6 ifCType
won't work?
Solution
As 40 you may have discovered, VB6 does not have 39 a CType
function like VB.NET does. However, the 38 other conversion functions (those that have 37 names beginning with C
), which you may have 36 encountered in VB.NET code, such as CInt 35 and CStr, do exist in VB6, and you can use 34 them to convert to and from non-object types. There 33 is no built-in function for converting an 32 object of one class to an object of another 31 class. Keep in mind that VB6, unlike VB.NET, does 30 not support inheritance. A class in VB6 29 can implement one or more interfaces, but 28 it cannot inherit from another class. However, if 27 a object's class implements more than one 26 interface, you can use the Set
statement to 25 cast an object to one of the interfaces 24 it supports (as Ant suggested). An extended 23 version of Ant's code example is provided 22 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 21 string or numeric expression.
CByte
Returns
Byte
Description
Convert 20 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 19 to 922,337,203,685,477.5807.
CDate
Returns
Date
Description
Convert 18 expression to Date.
Range for expression argument:
Any valid date expression.
CDbl
Returns
Double
Description
Convert 17 expression to Double.
Range for expression argument:
-1.79769313486232E308 to -4.94065645841247E-324 16 for negative values; 4.94065645841247E-324 15 to 1.79769313486232E308 for positive values.
CDec
Returns
Decimal
Description
Convert 14 expression to Decimal.
Range for expression argument:
+/-79,228,162,514,264,337,593,543,950,335 13 for zero-scaled numbers, that is, numbers 12 with no decimal places. For numbers with 11 28 decimal places, the range is +/-7.9228162514264337593543950335. The 10 smallest possible non-zero number is 0.0000000000000000000000000001.
CInt
Returns
Integer
Description
Convert 9 expression to Long.
Range for expression argument:
-32,768 to 32,767; fractions are 8 rounded.
CLng
Returns
Long
Description
Convert expression to Long.
Range for expression argument:
-2,147,483,648 7 to 2,147,483,647; fractions are rounded.
CSng
Returns
Single
Description
Convert 6 expression to Single.
Range for expression argument:
-3.402823E38 to -1.401298E-45 5 for negative values; 1.401298E-45 to 3.402823E38 4 for positive values.
CStr
Returns
String
Description
Convert expression to String.
Range for expression argument:
Returns 3 for CStr depend on the expression argument.
CVar
Returns
Variant
Description
Convert 2 expression to Variant.
Range for expression argument:
Same range as Double for numerics. Same 1 range as String for non-numerics.
Let's say you have an object of ChildClass 7 (child) that you want to cast to BaseClass. You 6 do this:
Dim base As BaseClass
Set base = child
Because of the way VB6 handles compile-time 5 type safety, you can just do that without 4 any extra syntax.
Note: Given that everyone 3 else seems to have mentioned CType
, I may just 2 have misunderstood the question completely, and 1 I apologise if that's the case!
The casts already mentioned are correct, but 7 if the type is an Object then you have to 6 use "Set" in VB6, such as:
If IsObject(Value) 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 5 on the type you are casting to. Almost all 4 user classes are objects as well as Collection, Dictionary, and 3 many others. The built-in types such as 2 long, integer, boolean, etc. are obviously 1 not objects.
Ctype() I believe. The C* (CDate(), CStr(), etc) are 1 holdovers for the most part.
Conversions are not "casts" at all. For 3 example try:
MsgBox CLng(CBool(3&))
The result is -1, not 3. This 2 is because those are conversion functions, not casts. Language 1 is important!
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.