Matt
Matt

Reputation:

Inheriting a .net class

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?

Upvotes: 1

Views: 955

Answers (3)

HanClinto
HanClinto

Reputation: 9461

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.

Upvotes: 0

Scott Dorman
Scott Dorman

Reputation: 42516

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.

Upvotes: 0

Charles Bretana
Charles Bretana

Reputation: 146499

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

Upvotes: 10

Related Questions