473,702 Members | 2,219 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 6723
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
9523
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 experts, can I check the type of a void pointer or use some guaranteed way to elicit an exception...
5
3150
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 is used to cast to a base class.
1
16232
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 derived object unless otherwise the function in the base class are declared as virtual and are...
5
5912
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
7956
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
3545
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
3003
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 because I thought C-style casts always compiled even if they wouldn't run correctly at runtime. I...
6
1407
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
1781
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 Base .... End Class
0
8652
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9089
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8983
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8940
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5907
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.