I am trying to build a chart to help me track what gets called when
in the inheritance chain. I came across an error that has me puzzled.
I have a derived class that inherits from a base class, but when I declare
a variable of the derived type, I can't assign a base type to it.
I am puzzled because the derived type 'is a' base type, so why can't a
variable of the derived type be assigned a value of base type?
For brievity, here is the offending line (Strict on or off makes no
difference)
Dim parent As New Base
Dim daughter As Derived
daughter = parent ' Error : Cast not valid
Here are the actual Base and Derived classes:
Public Class Base
Public Sub AAA()
Console.WriteLine("Base:AAA")
End Sub
Public Sub BBB()
Console.WriteLine("Base:BBB")
End Sub
Public Overridable Sub CCC()
Console.WriteLine("Base:CCC")
End Sub
End Class
Public Class Derived
Inherits Base
Public Overloads Sub BBB()
Console.WriteLine("Derived:BBB")
End Sub
Public Overrides Sub CCC()
Console.WriteLine("Derived:CCC")
End Sub
Public Sub DDD()
Console.WriteLine("Derived:DDD")
End Sub
End Class
I have tried commenting everything out of the derived class except
the Inherits line, and it still did not work. Am I mistaken in thinking
that it should work?
LFS 5 1200
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... I am trying to build a chart to help me track what gets called when in the inheritance chain. I came across an error that has me puzzled. I have a derived class that inherits from a base class, but when I declare a variable of the derived type, I can't assign a base type to it.
I am puzzled because the derived type 'is a' base type, so why can't a variable of the derived type be assigned a value of base type?
For brievity, here is the offending line (Strict on or off makes no difference)
Dim parent As New Base Dim daughter As Derived
daughter = parent ' Error : Cast not valid
--- Thats because the variable 'parent' contains an instance of type 'Base'
while the variable 'daughter' is of type 'derived'.
The problem here is that 'an apple is always a fruit but a fruit is not
always an apple'. So when you are doing something like
parent = daughter its always going to work because the the derived class
always implements what the base class implements
by way of inheritance. But when you attempt to do the above, the compiler
cannot figure out how to attempt to fill in the
implementation that a derived class might have implemented from the base
class since the base class has none of that
information !
I think what you are trying to do is something like:
Dim parent as Base
parent = New Derived
Dim daughter as Derived
daughter = DirectCast(parent, Derived) 'assuming Option Strict On
Note the difference between this and what you were attempting - the parent
is of type Base but is instantiated to the type
Derived.
Remember that the reverse cast will always work - that is:
Dim parent as Base
Dim daughter as New Derived
parent = daughter 'this always works without cast even with Option Strict
On
hope this helps - that is if my explanation has not confused you :)
Imran. Here are the actual Base and Derived classes:
Public Class Base Public Sub AAA() Console.WriteLine("Base:AAA") End Sub Public Sub BBB() Console.WriteLine("Base:BBB") End Sub Public Overridable Sub CCC() Console.WriteLine("Base:CCC") End Sub End Class
Public Class Derived Inherits Base Public Overloads Sub BBB() Console.WriteLine("Derived:BBB") End Sub Public Overrides Sub CCC() Console.WriteLine("Derived:CCC") End Sub Public Sub DDD() Console.WriteLine("Derived:DDD") End Sub End Class
I have tried commenting everything out of the derived class except the Inherits line, and it still did not work. Am I mistaken in thinking that it should work?
LFS
On Mon, 26 Jul 2004 22:11:58 -0500, Larry Serflaten wrote: I am trying to build a chart to help me track what gets called when in the inheritance chain. I came across an error that has me puzzled. I have a derived class that inherits from a base class, but when I declare a variable of the derived type, I can't assign a base type to it.
I am puzzled because the derived type 'is a' base type, so why can't a variable of the derived type be assigned a value of base type?
For brievity, here is the offending line (Strict on or off makes no difference)
Dim parent As New Base Dim daughter As Derived
daughter = parent ' Error : Cast not valid
Larry, this actually makes sense when you think about it... And there are
good technical reasons why this is the case (in fact, it is the case in
C++, C#, and Java as well). Think about it - if the compiler let you
assign a reference to a parent object to a daughter object what do you
think would happen when you tried to call DDD? The reference stored in
daughter would be a reference to a parent object that does not define DDD.
That would be big trouble :)
Here are the actual Base and Derived classes:
Public Class Base Public Sub AAA() Console.WriteLine("Base:AAA") End Sub Public Sub BBB() Console.WriteLine("Base:BBB") End Sub Public Overridable Sub CCC() Console.WriteLine("Base:CCC") End Sub End Class
Public Class Derived Inherits Base Public Overloads Sub BBB() Console.WriteLine("Derived:BBB") End Sub Public Overrides Sub CCC() Console.WriteLine("Derived:CCC") End Sub Public Sub DDD() Console.WriteLine("Derived:DDD") End Sub End Class
I have tried commenting everything out of the derived class except the Inherits line, and it still did not work. Am I mistaken in thinking that it should work?
Yes. This can't work for the reasons above. Just think about what would
happen if the compiler allowed this and you wrote code like:
Dim parent As New Base
Dim daughter As Derived = parent
parent.DDD() ' blamo!
It is perfectly valid to go the other way: parent = daughter, since
daughter contains all of the implementation of parent.
HTH
--
Tom Shelton [MVP]
"Larry Serflaten" <se*******@usinternet.com> wrote I am puzzled because the derived type 'is a' base type, so why can't a variable of the derived type be assigned a value of base type?
OK, Thanks Imran and Tom. I am actually running through all possible
combinations, just to verify which routines will be used:
Dim parent As New Base
Dim child As New Derived
Dim son As Base
Dim daughter As Derived
' Call all methods on parent
' Call all methods on child
son = parent
' Call all methods on son
son = child
' Call all methods on son
daughter = parent ' Error : Cast not valid
' Call all methods on daughter
daughter = child
' Call all methods on daughter
As you saw, I am not getting a full understanding of this inheritance,
especially as it pertains to Overrides, and Overloads, and the rest.
Hopefully if I can see it all laid out in a chart, I may then see a pattern
that will help to solidify how it all works....
LFS
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
As I understand it
- oh come on; don't laugh ;-)
Overloads - adds a "variation" of a routine that already exists in the
base class but that takes different argument(s), something like
[base]
Public Function Add( _
ByVal x as Long, _
, ByVal y as Long _
) as Long
[derived]
Public OverLoads Function Add( _
ByVal x as Long, _
, ParamArray y() as Long _
) as Long
Overrides - actively replaces a routine in the base class with an
alternative implementation for use in/by the derived class as in.
[base]
Public Overrideable Sub SetColours()
Me.BackColor = Color.White
End Sub
[derived]
Public Overrides Sub SetColours()
Me.BackColor = Color.Green
End Sub
HTH,
Phill W.
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote Overloads - adds a "variation" of a routine that already exists in the base class but that takes different argument(s), something like
<...> Overrides - actively replaces a routine in the base class with an alternative implementation for use in/by the derived class as in.
That seems to be the most of it, but there is also the quirky response
of using Me in the base class when it is used to call on some other
public member of the base class.
I am working on getting my facts straight, before I try to say how Me
responds in those cases....
LFS This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vinodh Kumar |
last post by:
I see that casting changes the value of a pointer in case of multiple
inheritance.In single inheritance also it is the same know?Isn't it?
Vinodh Kumar P
|
by: coeng |
last post by:
I am having trouble comprehending the use of the four casting
operators in C++.
Does anyone know of a URL that provides a very detailed, yet very
simple, explanation of what they accomplish and...
|
by: David Rager |
last post by:
Howdy,
Put briefly, I have an array of chars, which I would like to access in pairs
of bytes via casting the array to an array of shorts. I'm trying to be as
elegant as possible.
Below is a...
|
by: Jakob Bieling |
last post by:
Hi,
I have a question about casting and using the casted pointer: Suppose I
have a 'base' class and a 'derived' class (which is derived from 'base').
Now I have this scenario:
base* p1 = new...
|
by: macluvitch |
last post by:
Hello folks,
During developping I've met a problem this is how it looks like
I have an expression like this
(type *)var + 1
Wath heppens is I want to make a pointer to this
|
by: ytrama |
last post by:
Hi,
I have read in one of old posting that don't cast of pointer which
is returned by the malloc. I would like to know the reason.
Thanks in advance,
YTR
|
by: Jim Bancroft |
last post by:
Hi everyone,
I'm having some trouble with the code below. I receive a compile-time
error on the second line saying "; expected":
private static void myTestFunction(long myLong)
{
...
|
by: quantumred |
last post by:
I found the following code floating around somewhere and I'd like to
get some comments.
unsigned char a1= { 5,10,15,20};
unsigned char a2= { 25,30,35,40};
*(unsigned int *)a1=*(unsigned int...
|
by: S. Lorétan |
last post by:
Hi guys,
Sorry for this stupid question, but I don't know why it isn't working.
Here is my (example) code:
namespace Test {
class A {
public string Label1;
}
|
by: newbtemple |
last post by:
Hey all, having some trouble with casting values inputed into a text box into double. In particular, I'm having trouble with the decimal. It's ok if someone puts in a number with a decimal when...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
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...
| |