473,378 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

newby - declaring a descendent class

I want to declare a set of descendent exception classes that contains no
extra code to the base. It's so I can test the class type of exception and
handle its error code differently depending on the exception that was
thrown.

I come from a delphi background so for this I would have something like

type ClassA = class
public
constructor create(errormessage : message; errorcode : integer);
end;

type ClassB = classA;
type ClassC = classA;

I don't seem to be able to get the syntax correct for C#. The following are
examples of what Ive tried so far. Ive looked for book examples but they
always seem to show derived classes that have extra functionality.
public class LastErrorException : CommException;

public class LastErrorException : CommException{};

public class LastErrorException = CommException;

public class LastErrorException = CommException{};

thanks
Nov 16 '05 #1
3 1204
Claire <bl****@blahhhhh.com> wrote:
I want to declare a set of descendent exception classes that contains no
extra code to the base. It's so I can test the class type of exception and
handle its error code differently depending on the exception that was
thrown.

I come from a delphi background so for this I would have something like

type ClassA = class
public
constructor create(errormessage : message; errorcode : integer);
end;

type ClassB = classA;
type ClassC = classA;

I don't seem to be able to get the syntax correct for C#. The following are
examples of what Ive tried so far. Ive looked for book examples but they
always seem to show derived classes that have extra functionality.
public class LastErrorException : CommException;

public class LastErrorException : CommException{};

public class LastErrorException = CommException;

public class LastErrorException = CommException{};


You can't have semi-colons outside the class definition. You want:

public class LastErrorException : CommException{}

To guide your thinking in future, the way to come up with the above
would be to take a normal class with extra functionality, and just get
rid of the extra functionality.

Note that that will only give you a public parameterless constructor -
constructors aren't inherited in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi Jon

I'm sorry, I don't think I understand what you mean.

I tried using your code change but Im still receiving the error described
below.
"C:\Projects\testvsproj\WindowsApplication1\CCompo rtM.cs(74): No overload
for method 'CommException' takes '0' arguments
"
When you say "constructors aren't inherited" do you have to re-create
constructor code for all descendents of a particular class? Doesn't that
defeat the point of inheritence? I've been reading through my reference book
and it doesn't mention anything about this.

I don't understand the following text at all
"To guide your thinking in future, the way to come up with the above
would be to take a normal class with extra functionality, and just get
rid of the extra functionality."
Nov 16 '05 #3
Claire <bl****@blahhhhh.com> wrote:
I'm sorry, I don't think I understand what you mean.

I tried using your code change but Im still receiving the error described
below.
"C:\Projects\testvsproj\WindowsApplication1\CCompo rtM.cs(74): No overload
for method 'CommException' takes '0' arguments
"
That means that the compiler was trying to create a default
constructor, but the default constructor would have called a
parameterless constructor in the base class - and there wasn't one.
When you say "constructors aren't inherited" do you have to re-create
constructor code for all descendents of a particular class?
No - but every constructor needs to explicitly call a base constructor
(or another "this" constructor) - or let the compiler explicitly call a
parameterless base constructor, if there is one.
Doesn't that
defeat the point of inheritence? I've been reading through my reference book
and it doesn't mention anything about this.
See http://www.pobox.com/~skeet/csharp/constructors.html
I don't understand the following text at all
"To guide your thinking in future, the way to come up with the above
would be to take a normal class with extra functionality, and just get
rid of the extra functionality."


That was slightly dodgy, as for some reason you *can* have a semi-colon
outside the class. (I didn't try before, I'm afraid.)

My suggestion to find out the syntax was to look at an existing
example, eg

class Foo : Bar
{
void DoSomethingExtra()
{
}
}

and remove the bits you didn't want - in this case, the extra method -
to leave:

class Foo : Bar
{
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andy Fish | last post by:
hi, I am using descendent selectors to format cells within a table according to the css class of the table. However, when using nested tables, it seems to pick up the outer matching rule rather...
3
by: Marco Rego | last post by:
How do I know if a class is descendent of another one, not necessaraly his direct parent ? In my case, I need to know if a certain class descends from Control. Thanks for any help. _____ Marco
3
by: Bernardo | last post by:
Hi, I had to convert a Win32 EXE project with some forms and classes into a DLL Project. When I instace the new DLL project from a test program I see all forms and classes... There is any way to...
0
by: L Scott | last post by:
I've created a page ancestry (framework) for our application so that all pages in our app would have the same look and feel. To do this, I created several user controls (header, footer, menu,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.