469,946 Members | 1,681 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

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 1470
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 discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.