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
|
|