tags:

views:

233

answers:

5

Ok here's what I'm trying to do I want to write a class that inherits everything from the class ListItem

class RealListItem : ListItem
{

  public string anExtraStringINeed;

}

For some reason or another .net is treating all the members like they are private when I try to do this so my class is worthless.

I tried to do a work around and do it like this:

class RealListItem : ListItem
{
  public string anExtraStringINeed;
  public ListItem list;
}

but it still doesn't work because I need to use it in a function that uses the accepts type ListItem and RealListItem isn't playing nice like it should. I can do aRealListItem.list but that only passes the list from RealListItem I need the whole object to be passed along.

Whats with this am I doing something wrong or will microsoft just not let you inherit .net classes?

A: 

yes I actually tried that first then went to class i was also try to find a way to put it before ListItem but couldn't find a way to make that work. I really need to get another C# book.

use comments instead of posting new answers
spoon16
+10  A: 

The System.Web.UI.Controls.ListItem class is "sealed"... That means you cannot inherit from it...

Charles Bretana
Any idea why this class is sealed? Why would it be desirable to prevent inheriting it?
Adam Lassek
Sealing classes is pure evil.
P Daddy
Not off the top of my head... but (this is advanced) you can control how this .Net control is rendered into the html, and add whatever you want to in the way of javascript properties or events to the html element that will be created from the ListItem when it is rendered to the browser...
Charles Bretana
On the contrary, sealing a type saves the large amount of work required to make that type inheritable in a usable manner. Designing a type for reuse is roughly 3 times as expensive as designing a type for your own use.
RoadWarrior
Yup. They didn't want to spend time making it right so they made it sealed. That's an inexcusable thing to do on a public API. Kinda defeats the purpose of making an OO framework when you seal its classes.
P Daddy
@RoadWarrior: where did this number come from? I loathe sealed classes... and have never noticed that 'designing for inheritance' was at all difficult. What am i missing?
Steven A. Lowe
Steven, When a nethod can be inherited from, and you put implementation details in a method that the class depends on, you should write code to protect the data structures from adverse effects of someone deriving from yr class and doing something that conflicts with that purpose.
Charles Bretana
This can be complex, depending on how interconnected these dependencies are. The only easy solution is to make the class sealed.
Charles Bretana
A: 

Thats good to know don't guess their any workaround.

A: 

As Charles states, ListItem is sealed which means you can't inherit from it. In the framework, some classes are marked as sealed and others are not. (The ones that aren't sealed you can inherit.)

You could create your own ListItem that contains a System.Web.UI.WebControls.ListItem, but I don't think this will actually provide you much benefit.

What is it that you are trying to accomplish? You may need to find an alternate way to do things.

Scott Dorman
A: 

For the extra string you need, you may be best off making a custom object with all of the extra data you need, and storing that in the ListItem's "tag" field -- that's what it's there for.

HanClinto