Inheriting Exception Classes 
Author Message
 Inheriting Exception Classes

When I create a class that inherits from System.ApplicationException, the
derived class does not seem to inherit all the constructors.

Class MyException
        Inherits System.ApplicationException
        Public ErrorCode as Integer
End Class

When I try to create a new instance of the class with the message parameter
in the constructor as follows:

Dim ex as MyException = new MyException("Message text")

The compiler tells me that there are too many arguments to Public Sub New()

It seems that the compiler only recognizes the default constructor and not
the others.

Why doesn't it inherit the other constructors from
System.ApplicationException?

Thanks

Eugene Olsen



Wed, 04 Aug 2004 11:13:13 GMT  
 Inheriting Exception Classes
Eugene,

Although classes inherit all the methods of a base class, constructors are
the exception to the rule. However, you don't have to redeclare the default
constructor because the compiler will do this for you if it sees that you
left it out (but you don't ever see this code in your program). You have to
redeclare all other contructors yourself and call them using
'MyBase.New(...)' for each one.

Since we are on the subject of constructors, here are some interesting
points you should be aware of:
1) If a class doesn't redeclare ANY constructors, the compiler will
automatically declare it for you and call the base constructor.
2) If you declare one or more constructors, but DON'T declare a default
constructor, then the compiler will assume you did this for a reason and not
declare the default constructor for you.
3) When calling the constructor of the base class, use the MyBase.New(...)
statement and put it as the first line in your constructor.
4) When creating a dispose method in a class, call the MyBase.Dispose()
statement as the last line in your method. It is good practice to clean up
your own object before asking the base classes to clean up themselves.

Take care,

Brian Bischof, CPA, MCSD, MCT
Converting from VB6 to VB.NET? It just got easier...
"The .NET Languages: A Quick Translation Guide"
www.bischofsystems.com/CSharp.asp



Quote:
> When I create a class that inherits from System.ApplicationException, the
> derived class does not seem to inherit all the constructors.

> Class MyException
>         Inherits System.ApplicationException
>         Public ErrorCode as Integer
> End Class

> When I try to create a new instance of the class with the message
parameter
> in the constructor as follows:

> Dim ex as MyException = new MyException("Message text")

> The compiler tells me that there are too many arguments to Public Sub
New()

> It seems that the compiler only recognizes the default constructor and not
> the others.

> Why doesn't it inherit the other constructors from
> System.ApplicationException?

> Thanks

> Eugene Olsen



Thu, 05 Aug 2004 17:08:29 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Problem with a vb.Net class inheriting from a c# class

2. nested class inherits containing class accessing private instance members

3. Identifying Inherited Classes Properties and Methods seperately form inheriting classes P's and M's

4. DataGrid remove item error bind with own class inherit from collection

5. DataGrid remove item error bind with own class inherit from collection

6. Hide property in inherited class?

7. Returning a collection class (inherited from CollectionBase) via webService

8. Custom inherited class...

9. Inheriting the commondialog class in Vb.net

10. two inherits in one class

11. Inherit WSDL generated vb into COM class

12. Class inherit issues

 

 
Powered by phpBB® Forum Software