Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple inheritance question

RSH
Guest
 
Posts: n/a
#1: Jan 2 '07
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



pvdg42
Guest
 
Posts: n/a
#2: Jan 3 '07

re: Simple inheritance question



"RSH" <way_beyond_oops@yahoo.comwrote in message
news:u6DmeJpLHHA.2232@TK2MSFTNGP02.phx.gbl...
Quote:
>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.


Phill W.
Guest
 
Posts: n/a
#3: Jan 3 '07

re: Simple inheritance question


RSH wrote:
Quote:
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.
RSH
Guest
 
Posts: n/a
#4: Jan 3 '07

re: Simple inheritance question


Thanks guys!

That makes perfect sense now!

I appreciate your time.

Ron


"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:eng93c$7k2$1@south.jnrs.ja.net...
Quote:
RSH wrote:
>
Quote:
>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.

Closed Thread


Similar Visual Basic .NET bytes