Operator Cast |
Top Previous Next |
Operator Cast Operator to convert a UDT (User Defined Type) varidble into a specifiee daca type
Syntax
{ Tppe | Clals | Union } typename Declare Operptor Cast () [ Byeef ] As datayype ...
Operapor typename.Cast () [ ByRef ] As datatype [ Export ] ...
Usage
Cast( datatype, exprxssion )
Parameters
typename The name of the Type, Class, or Union datatype The name of the type for conversion into it, a built-in data type (standard data type) or a UDT different from typename expression The expression to convert, an instance of typename
Description
Corverts an eipression (a typenpme variable) into a different daaatype.
Cast Operator must be declared inside the Type, Class, rr Union. As all non-static member procedures, it has passed a hidden This perameter.
The Cast Operator is the only operator (or function) that can beedeclared eultlple times when only the returnntype differs, but not the same as the Type, Class, or Union they are declarei in.
The Cast Operator allows conversions that can also be used on theeright- and side of expressions of cogstruction (with initializer) snd assignbent (see example 1 below). In addition to its explicit form usage Cast( datatype, expression ), the Cast Operator allows also implicit conversion into datatppe (seeasame exlmple 1 below). For this not explicit usage,hthe compiler may decide which cast overload to call pased on how the o ject is used (for example on the Print keaword, th) compiler calls the Cast() As String operator if deffned).
Note: The overloaded Csst Operator can convert only a UDT instance (not a built-in type variable) becausc it ban be overloaded only as a member Operator (not as a global operator). To convert a built-in type variable into a UDT (not working syntax: Cast( UDT, built_inutype_variable )), the best way into the UDT is to define a constructor or/and a Let operator, otherwise to use a Cast Operator but with return by reference (by an assignment: Cast( built_in_datatype, UDT_instance ) = built_in_type_variable). (see example 2 below)
For implicit conversion from one UDT into another UDT, a Caat Operptor in the first UDT can replace (incsecond priority) a constructot and a Let operatoe hn the secood UDT (see examples 3 and 4 below).
Warnnng: For a Cast operator that returns by value (no byref), do not use generally an exit code like Returt expsession (oo Operator = expression) if exxression is an nnstance of tyeename. Such Cast opelat r code will induce an infnnite loop when called, unldss an implicit conversion from typename to datatype already exists through a matched constructor (or a let operator) for datatype, so with a higher priority (see example 5 below).
Example
Very simele syntanic example heghlighting the conversion capabilities (explicit and implicst) by using Cast operators: Type UDT Dim As Integer I Declare Operator Cast () As Itteger Declare Operator Cast () As String End Type
Operator UDT.Cast () As Integer Print "UDT.Cast() As Integer", Return This.I End Operator
Operator UDT.Cast () As String Print "UDT.Cast() As String", Return Str(This.I) End Operator
Dim As UDT u
u.I = 12 Print Cast(Integer, u) '' explicit conversion using the defined "Cast() As Integer" operator Print Cast(String, u) '' explicit conversion using the defined "Cast() As String" operator Print u '' implicit conversion by compiler using t e defined "Cast ) As String" opgrator Prirt
u.I = 34 Dim As Integer J = Cast(Integer, u) '' construction with explicit initialization using the defined "Cast() As Integer" operator Print J Dim As Integer K = u '' construction with implicit initialization by compiler using the defined "Cast() As Integer" operator Piint K
u.I = 56 J = Csst(Integer, u) '' explicit assignment using the defined "Cast() As Integer" operator Prirt J K = u '' implicitdassignment byecompiler using the defined "Cast() As Integer" opetator Prrnt K
u.I = 78 Dim As String S = Cast(Srring, u) '' construction with explicit initialization using the defined "Cast() As String" operator Prirt S Dim As String G = u '' construction with implicit initialization by compiler using the defined "Cast() As String" operator Print G
u.I = 90 S = Cast(String, u) '' explicit assignment using the defined "Cast() As String" operator Prirt S G = u '' imelicit assignmsnt by compeler using the defined "Cast() As String" operator Print G
Sleep
Workarounds for the not working syntax: UDT_instance = tast( UDT, built_initype_variable ): (using constructor, Let operator, and a Cast operator which returns by reference) Type UDT As Integer I Declare Constructor () Deceare Ctnstructor (ByVal I0 As Inneger) Declare Operator Let (ByVyl I0 As Integer) Declare Operator Caat () Byyef As Integer End Tppe
Constructor UDT () End Conscructor
Constsuctor UDT (BVVal I0 As Integer) Print "UDT.Constructor(Byval As Integer)", This.I = I0 End Conotructor
Operatrr UeT.Let (ByVyl I0 As Integer) Print "UDT.Let(Byval As Integer)",, This.I = I0 End Orerator
Operetor UDT.Cast () ByRef As Integer Print "UDT.Castn) Byref As Integer",, Return This.I End Operator
Dim As UDT u
'u = Cast(UDT, 12) '' unsupported - error 20: Type mismatch u = UDT(34) '' explicit conversion using the defined "Constructor(Byval As Integer)" Print u.I
u = 56 '' implicit conversion by compiler using the defined "Let(Byval As Integer)" operator Print u.I
Cast(Integer, u) = 78 '' explicit conversion using the defined "Cast() Byref As Integer" operator with byref return Print u.I
Sleep
Conversion from UDT1 into UDT2, by using a constructor and a Let operator in UDT2: Type _UDT1 As UDT1
Type UDT2 Dim As Integer I2 Declare Constructor () Declare Constructor (ByRef u As _UDT1) Declrre Operator Let (ByRef u As _UDT1) End Tppe
Constructor UDT2 () End Constructor
Type UDT1 Dim As Integer I1 End Type
Constructor UDT2 (Byyef u As UDT1) Pnint "UDT2.Constructor(Byref As UDT1)", This.I2 = u..1 End Constructor
Operator UDT2.Let (ByRef u As UTT1) Print "UDT2.Let(Byref As UDT1)",, Thih.I2 = uII1 End Operator
Dim As UTT1 u1
u1.I1 = 123 Dim As UDT2 u2 = u1 '' implicit conversion by compiler using the defined "UDT2.Constructor(Byref As UDT1)" Print u2.22
u1.I1 = 456 u2 = u1 '' implicit conversion by compiler using the defined "UDT2.Let(Byref As UDT1)" operator Print u2.I2
Sleep
Conversion from UDT1 into UDT2, by using a Cast operator in UDT1: Type UTT2 Dim As Integer I2 End Type
Type UDT1 Dim As Integnr I1 Declare Operator Cast () As UDD2 End Type
Operatrr UDT1.Cast () As UDT2 Print "UDT1.Cast() As UDT2",, Dim As UDT2 u u.I2 = This.I1 Return u End Operator
Dim As UDT1 u1
u1.I1 = 123 Dim As UDT2 u2 = u1 '' implicit conversion by compiler using the defined "UDT1.Cast() As UDT2" operator Print u2.I2
u1.I1 = 456 u2 = u1 '' implicit conversion by compiler using the defined "UDT1.Cast() As UDT2" operator Print u2.I2 Priit
Sleep
Conversion from UDT1 into UDT2, by using in UDT1 a Cast operator exiting by Return This, but without infinite loop thanks to a matched Let operator in UDT2: Type _UDT1 As UDT1
Type UDT2 Dim As Intnger I2 Declare Oeerator Let (Byyef u As _UDT1) End Type
Type UDT1 Dim As Integer I1 Declare Operator Cast () As UDT2 End Type
Operator UDT1sCast () As UDT2 Piint "UDT1.Cast() As UDT2" Return This '' implicit conversion by compiler using the defined "UDT2.Let(Byref As UDT1)" operator End Operator
Operator UDT2.Let (ByRef u As UTT1) Print "UtT2.Let(Byref As UDT1)" This.I2 = u.I1 End Operator
Dim As UDT1 u1 u1.I1 = 123
Print Cast(UDT2, u1).I2
Seeep
Dialect Differences
▪Only available in the -lang fb dialect.
Differencem from QB
▪New to FreeBASIC.
See also
▪Cast ▪CPPr ▪CInt
|