Inheriting Existing Classes

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

I amd playing around with inheritance a little in VB.Net

If I create a new class which inherits from ListViewItem and I am only
wanting to override the ToString Method. In this situation, If I only
override the "ToString" method, I find that I only have a single
constructor. Do I have to clone all of the constructor methods and then
execute the MyBase.New(....) methods for each of those constructors? If I
am inheriting all methods and data, why would I have to re-create these
methods.

When I create an instance of this new class, it seems to create the new
class just fine, but when I go to add that object into my combobox I get a
"Object reference not set to an instance of an object." My code looks like
the following...

Dim item As New SpListViewItem(items(indx))
cboFormatType.Items.Add(item)

My "item" is populated as I expected, so I do not understand the error.

Any idea?

Thanks in advance for your assistance!!!!!!!!
 
I amd playing around with inheritance a little in VB.Net

If I create a new class which inherits from ListViewItem and I am only
wanting to override the ToString Method. In this situation, If I only
override the "ToString" method, I find that I only have a single
constructor. Do I have to clone all of the constructor methods and then
execute the MyBase.New(....) methods for each of those constructors? If I
am inheriting all methods and data, why would I have to re-create these
methods.

Constructors are not inherited. You don't need to call MyBase.New()
unless you want to call one of the parameterized constructors - since
the compiler will automatically call the base class default constructor
for you. But, if you need to pass arguments to your base class then you
need to do it with MyBase.New...
When I create an instance of this new class, it seems to create the new
class just fine, but when I go to add that object into my combobox I get a
"Object reference not set to an instance of an object." My code looks like
the following...

Dim item As New SpListViewItem(items(indx))
cboFormatType.Items.Add(item)

My "item" is populated as I expected, so I do not understand the error.

Any idea?

Hard to say with out seeing the code for the ToString method... Which
is likely the source of the error, since that is what the combobox will
call by default...
 
Jim,
In addition to Tom's comments.

Constructors are never inherited! As you may have the case where the derived
class requires a different set of constructors then the base class, hence it
is left up to the designer of the derived class to implement the correct set
of constructors for the derived class.

Basically its (IMHO) "cleaner" to need to add the constructors you need to
the derived class, rather then have some syntax to remove constructors that
may have been inherited and you don't need, especially when a new
constructor could be added to the base, and you missed "removing" that
constructor from the derived class...

Hope this helps
Jay
 
Back
Top