473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Shadowing Question

Hi,

Quick (and probably simple) question regarding shadowing and
polymorphism. Problem is best explained by an example.

Public Class A
public CommonProperty as Integer
end class

Public Class AB : Inherits A
Public Specific as String
End Class

Public Class AC : Inherits A
Public Exclusive as Boolean
End Class

Public ClassB
Public MemberObject as new A
End Class

Public Class BA : Inherits B
Public shadows MemberObject as new AB
End Class

Question:

If I treat an instance of class BA as an instance of Class B and then
assign MyInstance.MemberObject to an instance of AB which I have downcast to
an instance of A what happens? Does the compiler figure everything out and
raise errors if I try to upcast a polymorphed AC (as an A) to
BA.MemberObject ?

Thanks!

Nick



Nov 18 '05 #1
3 1589
Hi Nick:

The important concept to realize with the Shadows keyword is that a
new field will result in the class. If you assign to the field through
a base class reference you are actually assigning to the shadowed
field.

In other words:

Dim myBA As New BA
Dim myB As B

myB = CType(myBA, B)

myB.MemberObject = New AC

Console.WriteLine(myB.MemberObject.CommonProperty)
If myBA.MemberObject Is Nothing Then
Console.WriteLine("myBA.MemberObject is Nothing")
End If

Will spit out:

0
myBA.MemberObject is Nothing

We've assigned to the field B::A, leaving BA::A uninitialized.
Making sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 16 Nov 2004 11:33:02 -0000, "Nick Stansbury"
<ni************@sage-removepartners.com> wrote:
Hi,

Quick (and probably simple) question regarding shadowing and
polymorphism. Problem is best explained by an example.

Public Class A
public CommonProperty as Integer
end class

Public Class AB : Inherits A
Public Specific as String
End Class

Public Class AC : Inherits A
Public Exclusive as Boolean
End Class

Public ClassB
Public MemberObject as new A
End Class

Public Class BA : Inherits B
Public shadows MemberObject as new AB
End Class

Question:

If I treat an instance of class BA as an instance of Class B and then
assign MyInstance.MemberObject to an instance of AB which I have downcast to
an instance of A what happens? Does the compiler figure everything out and
raise errors if I try to upcast a polymorphed AC (as an A) to
BA.MemberObject ?

Thanks!

Nick




Nov 18 '05 #2
Thanks - that was basically what I'd feared. It's a limitation that you
can't override a function where the only difference is the return type - and
that makes the functionality I've described here impossible without getting
messy. Any ideas on how to do this?
Nick

The important concept to realize with the Shadows keyword is that a
new field will result in the class. If you assign to the field through
a base class reference you are actually assigning to the shadowed
field.

In other words:

Dim myBA As New BA
Dim myB As B

myB = CType(myBA, B)

myB.MemberObject = New AC

Console.WriteLine(myB.MemberObject.CommonProperty)
If myBA.MemberObject Is Nothing Then
Console.WriteLine("myBA.MemberObject is Nothing")
End If

Will spit out:

0
myBA.MemberObject is Nothing

We've assigned to the field B::A, leaving BA::A uninitialized.
Making sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 16 Nov 2004 11:33:02 -0000, "Nick Stansbury"
<ni************@sage-removepartners.com> wrote:
Hi,

Quick (and probably simple) question regarding shadowing and
polymorphism. Problem is best explained by an example.

Public Class A
public CommonProperty as Integer
end class

Public Class AB : Inherits A
Public Specific as String
End Class

Public Class AC : Inherits A
Public Exclusive as Boolean
End Class

Public ClassB
Public MemberObject as new A
End Class

Public Class BA : Inherits B
Public shadows MemberObject as new AB
End Class

Question:

If I treat an instance of class BA as an instance of Class B and then
assign MyInstance.MemberObject to an instance of AB which I have downcast toan instance of A what happens? Does the compiler figure everything out andraise errors if I try to upcast a polymorphed AC (as an A) to
BA.MemberObject ?

Thanks!

Nick



Nov 18 '05 #3
Hi Nick:

The approach in the Framework is to basically embed the return type
into the method for simple types. For instance, SqlDataReader has a
set of GetXXX(index) methods where you pass the index of a column and
it returns the value. GetBoolean, GetByte, GetInt32.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 16 Nov 2004 15:07:24 -0000, "Nick Stansbury"
<ni************@sage-removepartners.com> wrote:
Thanks - that was basically what I'd feared. It's a limitation that you
can't override a function where the only difference is the return type - and
that makes the functionality I've described here impossible without getting
messy. Any ideas on how to do this?
Nick

The important concept to realize with the Shadows keyword is that a
new field will result in the class. If you assign to the field through
a base class reference you are actually assigning to the shadowed
field.

In other words:

Dim myBA As New BA
Dim myB As B

myB = CType(myBA, B)

myB.MemberObject = New AC

Console.WriteLine(myB.MemberObject.CommonProperty)
If myBA.MemberObject Is Nothing Then
Console.WriteLine("myBA.MemberObject is Nothing")
End If

Will spit out:

0
myBA.MemberObject is Nothing

We've assigned to the field B::A, leaving BA::A uninitialized.
Making sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 16 Nov 2004 11:33:02 -0000, "Nick Stansbury"
<ni************@sage-removepartners.com> wrote:
>Hi,
>
> Quick (and probably simple) question regarding shadowing and
>polymorphism. Problem is best explained by an example.
>
>Public Class A
> public CommonProperty as Integer
>end class
>
>Public Class AB : Inherits A
> Public Specific as String
>End Class
>
>Public Class AC : Inherits A
> Public Exclusive as Boolean
>End Class
>
>Public ClassB
> Public MemberObject as new A
>End Class
>
>Public Class BA : Inherits B
> Public shadows MemberObject as new AB
>End Class
>
>Question:
>
> If I treat an instance of class BA as an instance of Class B and then
>assign MyInstance.MemberObject to an instance of AB which I have downcastto >an instance of A what happens? Does the compiler figure everything outand >raise errors if I try to upcast a polymorphed AC (as an A) to
>BA.MemberObject ?
>
>Thanks!
>
>Nick
>
>
>
>
>
>
>
>


Nov 18 '05 #4

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

Similar topics

3
by: Aguilar, James | last post by:
Hey all. I was making a newbie mistake that I eventually figured out. That is not my question. My question is about the error message. So let me set the stage for you: class Superclass {...
5
by: Dave Taylor | last post by:
I'm trying to derive a class from a typed DataSet to add some methods to one of the DataTables. I would like to keep the names the same in the derived class as in the base class, so I have used...
2
by: David Garamond | last post by:
Is there a feature similar to this currently in Postgres, or will there be? Sometimes (like in a shared hosting environment), we cannot have the luxury of hot-swapped RAID or expensive SAN, and...
1
by: John Salerno | last post by:
I understand that if you reassign a built-in name to your own variable, such as: str = 'hello' then you lose the use of the built-in (in this case str()), but is this also the case in terms...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.