Simple inheritance question

R

RSH

I have a simple question regarding inheritance in a web form.

I have a DropDownList in an aspx form. It is called DropDownList1

I have a class that will be overriding the render event so I have a snippet
of this class:
Public Class CustomDDL

Inherits DropDownList

Protected Overrides Sub render(ByVal writer As HtmlTextWriter)

For Each li As ListItem In Items

li.Text = li.Text & " - Test Render"

Next

End Sub

End Class

My question is ... what is the syntax to "convert" dropdownlist1 from a
dropdownlist to my CustomDDL?



Thanks!

Ron
 
P

pvdg42

RSH said:
I have a simple question regarding inheritance in a web form.

I have a DropDownList in an aspx form. It is called DropDownList1

I have a class that will be overriding the render event so I have a
snippet of this class:
Public Class CustomDDL

Inherits DropDownList

Protected Overrides Sub render(ByVal writer As HtmlTextWriter)

For Each li As ListItem In Items

li.Text = li.Text & " - Test Render"

Next

End Sub

End Class

My question is ... what is the syntax to "convert" dropdownlist1 from a
dropdownlist to my CustomDDL?



Thanks!

Ron
If I'm interpreting your question correctly, you can't.
Here's why:
Inheritance works in one direction only, from superclass to subclass.
Your DropDownList1 in an instance of the DropDownList class, which is the
superclass you are extending with your CustomDDL class. An instance of your
CustomDDL class is also an instance of the DropDownList class via
inheritance, but the existing object DropDownList1, an instance of the
superclass DropDownList, cannot be converted to the subclass (CustomDDL)
type.
You'll need to create an instance of the CUstomDDL class, which will inherit
the DropDownList class properties and behaviors.
 
P

Phill W.

RSH said:
My question is ... what is the syntax to "convert" dropdownlist1 from a
dropdownlist to my CustomDDL?

Short answer: You can't.

Longer Answer: You can't, and here's why.

The DropDownList class has a bunch of properties and methods.
Your CustomDDL, because it inherits from this, has /all/ of these, plus
any new stuff that you've added.

However, /because/ your CustomDDL has all the base class stuff, you can
"treat it as" a DropDownList, i.e. you can "up-cast" your control to
"get at" the base class functionality, as in

DirectCast( customDDL1, DropDownList ).whatever( ... )

The converse is /not/ True.
The DropDownList does /not/ have all the "stuff" that your customDDL
has, so you cannot "treat is as" one (i.e. you can't "down-cast" from
the base class to a subclass).

However ...

Object Variables are clever things these days. Not only can they hold
the /actual/ type that they are declared as, but they can hold an object
of any sub-class as well (because the subclass has /all/ the base class
stuff, plus bits, so can be "treated as" the base Type).
So, if in your base web page, you have

Protected WithEvents DropDownList1 as DropDownList

then there's nothing to stop you doing

MyBase.DropDownList1 = New CustomDDL

The base class is happy, because it has a reference to an object that it
can "treat as" a DropDownList, but when it tries to call, say, the
Render() method, it will be the /override/ of Render in your custom
subclass that actually gets called!

Welcome the Wonderful World of Polymorphism!

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top