473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can't down cast to base class

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
Nov 21 '05 #1
6 6714
Dim newControl As New TextBox

Dim mc As MyControl = CType(newContro l, Control)

"Lore Leunoeg" <lo*********@gm x.net> wrote in message
news:dj******** **@news01.versa tel.de...
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

Nov 21 '05 #2
"Mr Newbie" <he**@now.com > schrieb:
Dim newControl As New TextBox

Dim mc As MyControl = CType(newContro l, 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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
DirectCast? Could you send an example?

Thank you
Sincerely
Lore

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schrieb im Newsbeitrag
news:OO******** ******@TK2MSFTN GP09.phx.gbl...
"Mr Newbie" <he**@now.com > schrieb:
Dim newControl As New TextBox

Dim mc As MyControl = CType(newContro l, 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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
Dim newControl As New TextBox

Dim mc As MyControl = DirectCast(newC ontrol, Control)

Mr N.....

"Lore Leunoeg" <lo*********@gm x.net> wrote in message
news:dj******** **@news01.versa tel.de...
DirectCast? Could you send an example?

Thank you
Sincerely
Lore

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schrieb im
Newsbeitrag
news:OO******** ******@TK2MSFTN GP09.phx.gbl...
"Mr Newbie" <he**@now.com > schrieb:
> Dim newControl As New TextBox
>
> Dim mc As MyControl = CType(newContro l, 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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Nov 21 '05 #5
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/de...verloading.asp

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

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Lore Leunoeg" <lo*********@gm x.net> wrote in message
news:dj******** **@news01.versa tel.de...
| 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
|
|
Nov 21 '05 #6
"Lore Leunoeg" <lo*********@gm x.net> wrote in message
news:dj******** **@news01.versa tel.de...
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.

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
9474
by: Damon | last post by:
Hi, I created a template class and stored its instances in a map object that is like std::map<std::string, void* >. But i just made a nasty discovery that if i fail to use a C-style cast properly with the void pointer to return it to the proper class it belongs to, I get very bad and random segmentation fault. So question to all the...
5
3136
by: Ingo Nolden | last post by:
Hi there, I am writing a smart pointer that is similar to the boost intrusive ptr. I am trying to make it behave like a c++ pointer including the implicit and explicit casting behaviour. Below is the part more or less relevant to my problem. Version 1 is fine for casting to a derived class, but it must be called explicitly even though it...
1
16222
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition to the fact1 is given as since the derived object always consists of the base part , the base class pointer will always point to the base part in the...
5
5905
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int Count { get { return 0; }
1
7950
by: conchur | last post by:
Is there an elegant way to have a class utilise an inherited method to return a Typed Collection of itself? Example: class MyList<T> : List<T> where T : MyRoot { Add... Remove... }
6
3536
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
5
2993
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran into a problem using C-style casting to an instance of a derived class from its interface type to a variable of its concrete type. I was confused...
6
1399
by: Ron M. Newman | last post by:
Hi, Under what circumstances can I ever get an exception about not being able to cast type "ABC" to type "ABC" if there's only one ABC class in the system and it's loaded from a dynamic assembly? Thanks Ron
11
1769
by: Dinsdale | last post by:
I am trying to determine what the current cast of an object is. Essentially, if I use GetType, it always returns the original type of the object created but I want to know what the current cast of the object is (i.e.: is it still cast as a Derived class, or is it cast as a Base class?) Here is some pseudo code to show the example. Class...
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.