Implicit Typecast Operator Overloading?

  • Thread starter Thread starter Joe Duchtel
  • Start date Start date
J

Joe Duchtel

Hello -

I have the following scenario ...
Class cBase
End Class

Class cChild
Inherits cBase
End Class

Then there is a function that returns an instance of cBase() ...
Function getStuff() As cBase()

I would like to be able to do the following ...
Dim lDummy As cChild = getStuff() ' This would implicitly call the
typecast operator

.... without having to do an explicit CType() ...
Dim lDummy As cChild = CType(getStuff(), cChild)

I remember that in C++ I could overload the casting operator. This
was not possible with .NET 1.1 2003 but I heard somewhere that it
could be done in .NET 2.0 2008.

Is there any way to do that?

Thanks,
Joe
 
Joe Duchtel said:
Hello -

I have the following scenario ...
Class cBase
End Class

Class cChild
Inherits cBase
End Class

Then there is a function that returns an instance of cBase() ...
Function getStuff() As cBase()

I would like to be able to do the following ...
Dim lDummy As cChild = getStuff() ' This would implicitly call the
typecast operator

... without having to do an explicit CType() ...
Dim lDummy As cChild = CType(getStuff(), cChild)

This would fail because an instance of cBase is returned as you write above,
so I don't know how to help.
I remember that in C++ I could overload the casting operator. This
was not possible with .NET 1.1 2003 but I heard somewhere that it
could be done in .NET 2.0 2008.

Is there any way to do that?


Armin
 
Hello -

Okay ... I guess I oversimplified the actual problem in my example.
Here are a few more details ...

Class cA
Class cParent
End Class

Dim mParentInstance As New cParent

Public Overridable Function getStuff() As cParent
Return mParentInstance
End Function
End Class

Class cB
Inherits cA

Class cChild
Inherits cParent
End Class

Dim mChildInstance as New cChild

Public Overrides Function getStuff() As cParent
Return mChildInstance
End Function
End Class

Dim lDummy As New cA()

Dim lStuff As cChild = lDummy.getStuff()

I want to avoid having to do the following ...
Dim lStuff As cChild = CType(lDummy.getStuff(), cChild)

I hope this clarifies ...

Thanks,
Joe
 
Hello -

Okay ... I guess I oversimplified the actual problem in my example.
Here are a few more details ...

Class cA
Class cParent
End Class

Dim mParentInstance As New cParent

Public Overridable Function getStuff() As cParent
Return mParentInstance
End Function
End Class

Class cB
Inherits cA

Class cChild
Inherits cParent
End Class

Dim mChildInstance as New cChild

Public Overrides Function getStuff() As cParent
Return mChildInstance
End Function
End Class

Dim lDummy As New cA()

Dim lStuff As cChild = lDummy.getStuff()

I want to avoid having to do the following ...
Dim lStuff As cChild = CType(lDummy.getStuff(), cChild)

I hope this clarifies ...

=========

Yes, but I don't know how this could be done without casting. If lDummy was
declared as cB, I would suggest to declare cB.GetStuff as "Shadows", but in
this case it's not possible.


Armin
 
Hello -

Okay ... here is code that compiles and runs ...

Class cA
Class cBase
End Class

Class Blah
Inherits cBase

Public Name As String = "Blah"
End Class

Dim mBlahInstance As New Blah

Public Overridable Function getStuff() As cBase
Return mBlahInstance
End Function
End Class

Class cB
Inherits cA

Class cDerived
Inherits cBase

Public Name As String = "cDerived"
End Class

Dim mDerivedInstance As New cDerived

Public Overrides Function getStuff() As cBase
Return mDerivedInstance
End Function
End Class

Sub Main(ByVal aCommandArguments() As String)

Dim lFlightPlanSnap As cA = New cB()

Dim lStuffA As cB.cDerived = CType(lFlightPlanSnap.getStuff(),
cB.cDerived)

Console.WriteLine(lStuffA.Name)

....

My question is whether I can avoid doing the CType() by having a
conversion operator overload?

I hope this makes sense?

Thanks,
Joe
 
Joe Duchtel said:
My question is whether I can avoid doing the CType() by having a
conversion operator overload?

I hope this makes sense?

IMO no. That's what strict typing is all about. You can switch "Option
Strict Off" but I wouldn't recommend it.


Armin
 
You are still working against OOP.  As lFlightPlanSnap  is declared using
the cA type, you are telling that it should conform to this and in
particular it should return cBase for getStuff...

If declaring lFlightPlanSnap  as cB rather than cA is acceptable, I believe
you could use generics to achieve something similar to what you are trying
to do but it's difficult to know if it would fit your needs.

For example, my design would be :

Module Module1
    Class cBase
    End Class
    Class Blah
        Inherits cBase
        Public Name As String = "Blah"
    End Class

    Class cA(Of T As {New, cBase})
        Dim mBlahInstance As New T
        Public Overridable Function getStuff() As T
            Return mBlahInstance
        End Function
    End Class

    Class cDerived
        Inherits cBase

        Public Name As String = "cDerived"
    End Class

    Class cB
        Inherits cA(Of cDerived)
    End Class

    Sub Main(ByVal aCommandArguments() As String)

        Dim lFlightPlanSnap As cB = New cB()

        Dim lStuffA As cDerived = lFlightPlanSnap.getStuff()

        Console.WriteLine(lStuffA.Name)
    End Sub
End Module

Basically I create a base class who is able to use whatever type I want for
its "mBlahInstance" member rather than using a fixed type and trying to
change this after it has been previoulsy defined in the inheritance
hierarchy....

--
Patrice

"Joe Duchtel" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...























- Show quoted text -

Hello Patrice -

Thanks a lot for your suggestion. I was originally using .NET 1.1
(2003) where I cannot use generics but since I am already working on
moving to .NET 2.0 (2008) the generics will work. I will definitely
give that a try.

Thanks again!
Joe
 
Joe Duchtel wrote:
I have the following scenario ...
Class cBase
End Class

Class cChild
    Inherits cBase
End Class

Then there is a function that returns an instance of cBase() ...
Function getStuff() As cBase()

I would like to be able to do the following ...
Dim lDummy As cChild = getStuff()   ' This would implicitly call the
typecast operator

... without having to do an explicit CType() ...
Dim lDummy As cChild = CType(getStuff(), cChild)

I remember that in C++ I could overload the casting operator.  This
was not possible with .NET 1.1 2003 but I heard somewhere that it
could be done in .NET 2.0 2008.

Is there any way to do that?
<snip>

Yes and no.

Yes, you can define conversion operators in VB, but only for new
conversions. **But** the compiler assumes that there's already a
widening (implicit) conversion from a derived class to a base one and
a narrowing (explicit) conversion in the contrary direction.

Thus, no, you can't define a widening conversion (one that doesn't use
the Ctype operator) from a base class to a derived class, because
there's already a narrowing one (which requires the use of CType) pre-
defined.

It's as if you had in your code:

<example>
Class A
End Class

Class B: Inherits A
Shared Widening Operator CType(Value As B) As A
'Allows you to implicitly cast B to A
...
End Operator

Shared Narrowing Operator CType(Value As A) As B
'requires you to explicitly cast A to B
...
End Operator
End Class
</example>

Since both operators are already defined, the compiler won't let you
redefine one of then:

<example>
Class B: INherits A
...
Shared Widening Operator CType(Value As A) As B
... ops, syntax error
End Operator

End Class
</example>

HTH.

Regards,

Branco.
 
Well actually ,

I must admit that if i would have a verry good reasson to do so , i would
set it off at class level if absolutely necesarry
for instance i had once a project where i needed late binding to an ActiveX
executable .

So i created one class wich handled all the interaction to this late bound
interface and only in that one class i switched it off

Regards

Michel





"Joe Duchtel" <[email protected]> schreef in bericht
"ffO tcirtS noitpO" hctiws nac uoY

Better?

;)

Armin

Very funny :) ... I guess I could set Option Strict Off but I rather
keep it on ...

Thanks,
Joe
 
Well actually ,

I must admit that if i would have a verry good reasson to do so , i would
set it off at class level if absolutely necesarry
for instance i had once a project where i needed late binding to an ActiveX
executable .

So i created one class wich handled all the interaction to this late bound
interface and only in that one class i switched it off

Regards

Michel

"Joe Duchtel" <[email protected]> schreef in bericht


Very funny :) ... I guess I could set Option Strict Off but I rather
keep it on ...

Thanks,
Joe- Hide quoted text -

- Show quoted text -

Thanks to everyone for their responses!
Joe
 
Back
Top