tags:

views:

8502

answers:

6

What is the cast expression equivalent of VB.NET's CType in Visual Basic 6?

A: 

Ctype() I believe. The C* (CDate(), CStr(), etc) are holdovers for the most part.

StingyJack
Nope. There's no CType in VB6. The OP wanted the VB6 equivalent of the .NET CType function. Suggest you delete this answer
MarkJ
Nah, I dont need to alter history just to make myself look better. I just read the question backwards, though I could swear that I remember using it before.
StingyJack
+9  A: 

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

JohnFx
If you get rid of CType, then this answer is correct. The OP wanted the *VB6* equivalent of CType. The C* functions (CInt and friends) are available in both VB6 and VB.NET; however, CType is only available in VB.NET. There is no CType function in VB6...
Mike Spross
Also CBool, CByte, CCur, CDec, CSng and CVar.
onedaywhen
Good catch on the Ctype. Thanks.
JohnFx
+1  A: 

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.

Ryan
The OP asked for VB6. The example you posted is VB.NET.
Mike Spross
I didn't remember a CType cast, so it figures that it is VB.NET. However the statement about IsObject(myObject) is still valid and relevant. I was commenting to be aware that VB6 must use "Set" to assign objects to a variable, but you missed the point.
Ryan
+1. I think the crucial thing about your answer is that the built in implicit type conversion in VB6 does pretty much the same thing as CType(). It tries to cast, or for value types it tries to convert. So I think the real closest thing to CType in VB6 is just a straight assignment or Set statement.
MarkJ
+1  A: 

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!

Ant
+1. Actually, I think you're one of the few who understood the question ;-)
Mike Spross
It's easy enough to read something backwards ;)
Ant
+4  A: 

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 use CType in VB6, I get a "Sub or Function not defined" error. So, how can I perform casts in VB6 if CType 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.

Mike Spross
A: 

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!

Bob
True. However the question is poorly worded because CType does both casts and conversions. So the question is kind of half about casts and half about conversions. For instance CType("1",Integer) returns the Integer 1.
MarkJ