newby - declaring a descendent class

C

Claire

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
 
J

Jon Skeet [C# MVP]

Claire said:
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#.
 
C

Claire

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\CComportM.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."
 
J

Jon Skeet [C# MVP]

Claire said:
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\CComportM.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
{
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top