can't down cast to base class

L

Lore Leunoeg

Hello
I derived a class MyControl from the Control class.

Public Class MyControl
Inherits Control
Sub New()
MyBase.New()
End Sub
End Class

Now I want to do a down cast to the base class an I get an error.

Dim x As MyControl = New TextBox //wrong

Why can't I do this?

Thank you
Sincerely
Lore
 
H

Herfried K. Wagner [MVP]

Mr Newbie said:
Dim newControl As New TextBox

Dim mc As MyControl = CType(newControl, Control)

.... or you can use 'DirectCast' instead of 'CType' to make it more clear to
the reader that you are dealing with reference types.
 
M

Mr Newbie

Dim newControl As New TextBox

Dim mc As MyControl = DirectCast(newControl, Control)

Mr N.....
 
J

Jay B. Harlow [MVP - Outlook]

Lore,
In addition to the other comments.

| Public Class MyControl
| Inherits Control

| Dim x As MyControl = New TextBox //wrong

TextBox does not directly or indirectly inherit from MyControl, nor does
MyControl directly or indirectly inherit from TextBox. As MyControl inherits
directly from Control.

You cannot "down cast" TextBox to or from MyControl.

If MyControl inherited from TextBox, then you could cast a MyControl to a
TextBox variable, or you could down cast a TextBox variable, that contains a
MyControl object into a MyControl variable.

Public Class MyControl
Inherits TextBox

End Class

' implicit cast allowed, as MyControl "is a" TextBox
Dim x As TextBox = New MyControl

' explicit cast required, as "x" may only be a TextBox
' and not a MyControl...
Dim y As MyControl = DirectCast(x, MyControl)
Dim y As MyControl = CType(x, MyControl)

DirectCast only does a Cast, that is checks to see if "x" is that type &
allows the assignment.

While CType will do the Cast if the Cast is allowed, or it will attempt a
Conversion if one is defined, for example the CInt operator. Starting with
VS 2005 you will be able to define you own conversion operators (overload
the CType operator) on the types you define that CType will be able to use.
Which may mean that CType may convert a value when you really only wanted a
Cast (DirectCast) done...

http://msdn.microsoft.com/library/d...y/en-us/dnvs05/html/vboperatoroverloading.asp

http://msdn2.microsoft.com/en-us/library/yf7b9sy7

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello
| I derived a class MyControl from the Control class.
|
| Public Class MyControl
| Inherits Control
| Sub New()
| MyBase.New()
| End Sub
| End Class
|
| Now I want to do a down cast to the base class an I get an error.
|
| Dim x As MyControl = New TextBox //wrong
|
| Why can't I do this?
|
| Thank you
| Sincerely
| Lore
|
|
 
P

Phill. W

Lore Leunoeg said:
I derived a class MyControl from the Control class.

Now I want to do a down cast to the base class an I get an error.

Dim x As MyControl = New TextBox //wrong

because you're not downcasting. or upcasting.
these idea work with the "Parent" and "Child" in the Inheritance
relationship - here, you're actually dealing with, say, Brother and
Sister.

You can put either object (TextBox or MyControl) into a variable
dimensions "As Control" because, being derived from Control, the
Framework can do "Control" things with them (including assigning
them to a Control variable).

However, TextBox /added/ things (properties, methods, etc). to
Control as it was derived from it and, doubtless, so did you in
deriving MyControl. There is no guarantee that MyControl does
everything that TextBox does and vice versa, so the language
won't let you can't cast one to the other.

Dim x as MyControl = New TextBox ' Nope
Dim x as TextBox = New MyControl ' Nope

but

Dim x as Control = New TextBox
Dim x as Control = New MyControl

will both work.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top