DotNetTex

I have a question that nobody in my department is able to answer. It came to me while using the Class Designer to stub out our data access layer of our application. The question is:

If I create an abstract worker class as such:

public abstract class MyClass

{

public abstract void MyMethod(DataEntityAbstract Entity);

}

Then try the following in its derived class:

public class ThisClass : MyClass

{

// ThieDataEntity is derived from DataEntityAbstract class

public override void MyMethod(ThisDataEntity Entity)

{

// Do Something

}

}

Visual Studio 2005 will throw a compilation error. Why

I mean, polymorphism and inheritance allows us to declare a method using an abstract as a parameter. This allows the method to accept not only the abstract parameter which was declared, but any object derived from that abstract. It¡¯s the cornerstone of OOP.

So why can we not use these principles when declaring an abstract method I¡¯m sure you¡¯re probably asking yourself ¡°Why would you need to ¡± Maybe this will answer:

I need to create an abstract worker class, having helper methods as well as abstract methods. These abstract methods will expect a specific entity to be passed when they are called on the derived class, however the abstract class does not have foreknowledge as to WHICH specific entity will be required. Only that it will be derived from a known abstract class:

public abstract void ProcessData(EntityBase Entity);

It will be the responsibility of the derived class to specify WHICH specific entity:

public override void ProcessData(SQLEntity Entity)

{

//Do Something

}

The method on the derived worker class will only accept the SQLEntity, and will not allow use of any other entity which is derived from EntityBase (i.e. OracleEntity, AccessEntity, etc). The constraint exists because the derived worker class is to be used for SQL Server only, and thus only wants the SQLEntity entity!

Granted, I could always check the type and do a cast (or throw an exception), but why The principles of OOP should allow this.. right

DotNetTex



Re: Visual C# General Why is this not valid!!!???

Ross Dargan

Im not supprised this doesnt work - I doubt you could use an abstract item as a type since it can never be constructed.

It might be worth creating an interface and using that instead as the type, see if that works.

I havnt given a code sample, you clearly know your stuff - but if you would like me to flesh out what Im saying then let me know.

Enjoy

Ross (http://blog.the-dargans.co.uk)






Re: Visual C# General Why is this not valid!!!???

Marcelo Guerra - MSFT

Hi,

Interesting question. I think the answer is: it's ok that you cannot do that and it doesn't go against OOP.

In your first example, when non abstract class ThisClass inherits from MyClass it "accepts a contract" to implement the abstract methods (you can also override the non abstract non sealed ones if you want) but you must implement the abstract one. MyMethod in MyClass works for every object of type DataEntityAbstract, however MyMethod in ThisClass works only for ThisDataEntity objects, this leaves out other objects that are of type DataEntityAbstract and are not ThisDataEntity. In a sense you are not implementing that abstract method, just a part of it. A solution might be to have more than one override for the same method, but I don't think it would be easy to come up with a set of coherent rules to regulate this mechanism.

A different problem would be if you wanted to do:

public abstract class MyClass

{

public abstract DataEntityAbstract MyMethod();

}

Then try the following in its derived class:

public class ThisClass : MyClass

{

// ThieDataEntity is derived from DataEntityAbstract class

public override ThisDataEntity MyMethod()

{

// Do Something

}

}

In this case you are fully implementing the method and the fact that you are returning always the same type doesn't matter. This is not a cornerstone of OOP buy OPP allows it and C# doesn't. This may be a nice to have and it's called Covariant Return Types.





Re: Visual C# General Why is this not valid!!!???

Dick Donny

Hi

It's a compilation error because ThisDataEntity is not the same type as DataEntityAbstract and thus the method signatures differ.

Imagine some code working with the abstract type, yet receiving an instance of ThisClass ... it cannot call MyMethod because it doesn't know about the ThisDataEntity type .. it might try tp pass another type that inherits from DataEntityAbstract .

A better way of writing the abstract class would be with a generic type declaration with a restriction something like this ...

public abstract class MyClass<T> where T : DataEntityAbstract

{

public abstract void MyMethod(T Entity);

}


The inheriting class should then be able to declare itself thus .....

public class thisclass : myclass<thisdataentity>

Now you can override the method using the signature you seek.

Richard




Re: Visual C# General Why is this not valid!!!???

DotNetTex

Covariant Return Types... That's an interesting concept as well. It's kind of a misnomer when one says "C# was written for .NET" when in fact [looking under the hood] it's a language independent of the .NET framework. There are things which C# allows, but is not supported by the framework. And there are things which the framework allows but is not supported in C#.

Either way, however, I think it's a fantastic language. The only issue I (and most of my esteemed colleagues) have, is that Microsoft needs to slow down a bit in regards to changes. .NET 1.1 was an enhancement (and bug fix) for 1.0, but 2.0 caused ripples (especially in ASP.NET). 2.0 is barely implemented and now 3.0 is out, using the foundation classes for Vista. So much change so fast. Is this really a good thing Developers are running like heck trying to keep up, with a constant curve to overcome. I myself keep Amazon in business with all the books I buy. They barely collect dust before their obsolete due to yet another release of the .NET framework or Visual Studio... Is this change for the sake of change. Granted, each release offers more power and more features... But what is it really worth if we're still trying to absorb the changes from a previous release And lets get real... Companies simply do not turn on the same dime Microsoft does. I'm a contractor, and can say with some certainty that there are still companies out there using Windows NT and 2000. I just finished a contact with a LAEGE company who only now upgraded their workstations to XP Pro.. Their servers, however, will continue to run 2000... They do not embrace rapid change like Microsoft has adopted latey (regardless the hype from Paul). Also, this same company just finished an 8 million dollar .NET 1.1 project. Why 1.1 Because it was the version which was current when the project started.... How long before they make the change to 2.0 (or even 3.0) They need to see a nice ROI first!!

How can we ever become truly proficient. Lets face it. The .NET framework is VERY expansive. So much so that intellisense is now a "Must Have", rather then just the "Handy Thing" it used to be. As a developer, I like to feel very confident in my abilities and experience, yet Microsoft is making it very difficult lately to reach this state. I don't know. Back in the Visual Studio 6.0 days (sorry, had to mention it) things moved at a manageable pace. We saw enhancements, not drastic changes. We were allowed to gain the experience and knowledge necessary to provide well thought out solutions. But now... When presented with a task I can't help but wonder if I am doing it the way it should be. Or if there is a better way, which uses less resources and has better performance. Constant refactoring seems to be the norm.. Not that this is a bad thing, but it comes more often then I would like. Every day I am discovering something new in 2.0, and going back to utilize it for performance sake... Now there is 3.0 and Vista with the foundation framework... When will this slow down When can I actually have a chance to become proficient The guru has truly gone the way of the dinosaurs!!

We don't live in our mothers basement, glued to our monitors 36 hours in, sucking down cases of Mt Dew while eating cold, stale Domino's.. Stretching the very limits of science, caffine induced concepts, and physical consciousness (and their various levels).. We're responcible professional now. We have families, kids, hobbies.. and would like time to enjoy them (without having to do so with our nose burried in a Wrox book).

There are days when I find myself seriously considering a change to Java and J2EE. At least Sun keeps it fairly consistent and doesn't keep throwing curve balls at their developers. I bought a copy of Adobe Flex 2.0... so I'm half way there. I've been developing Microsoft applications sine the DOS days (before Windows).... I would miss it terribly... which is the only thing holding me back.. so far.

Thanks for your post. It was very insightful. I will pick up my soap box now and get back to reading... Say hi to Bill!

Brian Nichols





Re: Visual C# General Why is this not valid!!!???

Ross Dargan

erm, wow.

.net 2 is fantastic, a massive needed change from .net 1.1. And .net 3 is just bulding upon the solid .net 2 framework (and actualy really doesnt add anything apart from 4 (really cool) additions).

... Just to cheer you up .net 3.5 is out in the summer :-p

Have a good weekend!

Ross

DotNetTex wrote:

Covariant Return Types... That's an interesting concept as well. It's kind of a misnomer when one says "C# was written for .NET" when in fact [looking under the hood] it's a language independent of the .NET framework. There are things which C# allows, but is not supported by the framework. And there are things which the framework allows but is not supported in C#.

Either way, however, I think it's a fantastic language. The only issue I (and most of my esteemed colleagues) have, is that Microsoft needs to slow down a bit in regards to changes. .NET 1.1 was an enhancement (and bug fix) for 1.0, but 2.0 caused ripples (especially in ASP.NET). 2.0 is barely implemented and now 3.0 is out, using the foundation classes for Vista. So much change so fast. Is this really a good thing Developers are running like heck trying to keep up, with a constant curve to overcome. I myself keep Amazon in business with all the books I buy. They barely collect dust before their obsolete due to yet another release of the .NET framework or Visual Studio... Is this change for the sake of change. Granted, each release offers more power and more features... But what is it really worth if we're still trying to absorb the changes from a previous release And lets get real... Companies simply do not turn on the same dime Microsoft does. I'm a contractor, and can say with some certainty that there are still companies out there using Windows NT and 2000. I just finished a contact with a LAEGE company who only now upgraded their workstations to XP Pro.. Their servers, however, will continue to run 2000... They do not embrace rapid change like Microsoft has adopted latey (regardless the hype from Paul). Also, this same company just finished an 8 million dollar .NET 1.1 project. Why 1.1 Because it was the version which was current when the project started.... How long before they make the change to 2.0 (or even 3.0) They need to see a nice ROI first!!

How can we ever become truly proficient. Lets face it. The .NET framework is VERY expansive. So much so that intellisense is now a "Must Have", rather then just the "Handy Thing" it used to be. As a developer, I like to feel very confident in my abilities and experience, yet Microsoft is making it very difficult lately to reach this state. I don't know. Back in the Visual Studio 6.0 days (sorry, had to mention it) things moved at a manageable pace. We saw enhancements, not drastic changes. We were allowed to gain the experience and knowledge necessary to provide well thought out solutions. But now... When presented with a task I can't help but wonder if I am doing it the way it should be. Or if there is a better way, which uses less resources and has better performance. Constant refactoring seems to be the norm.. Not that this is a bad thing, but it comes more often then I would like. Every day I am discovering something new in 2.0, and going back to utilize it for performance sake... Now there is 3.0 and Vista with the foundation framework... When will this slow down When can I actually have a chance to become proficient The guru has truly gone the way of the dinosaurs!!

We don't live in our mothers basement, glued to our monitors 36 hours in, sucking down cases of Mt Dew while eating cold, stale Domino's.. Stretching the very limits of science, caffine induced concepts, and physical consciousness (and their various levels).. We're responcible professional now. We have families, kids, hobbies.. and would like time to enjoy them (without having to do so with our nose burried in a Wrox book).

There are days when I find myself seriously considering a change to Java and J2EE. At least Sun keeps it fairly consistent and doesn't keep throwing curve balls at their developers. I bought a copy of Adobe Flex 2.0... so I'm half way there. I've been developing Microsoft applications sine the DOS days (before Windows).... I would miss it terribly... which is the only thing holding me back.. so far.

Thanks for your post. It was very insightful. I will pick up my soap box now and get back to reading... Say hi to Bill!

Brian Nichols






Re: Visual C# General Why is this not valid!!!???

DotNetTex

I agree. 2.0 did introduce a much needed change... Mybe it's the age thing. Which is probably why most developers in my position have already made the move to administrative management. I've thought about it, but it would just kill me to lose the skillset which I have worked very hard to obtain. Besides.... I need to stay in the code. I would go nuts if I didn't.

It's just that the arc on that learning curve gets tighter and tighter with each release. Been doin this since 1977... I can remember when I used to eat this stuff up!! Clear a 4" thick tech book in 24 hours (and staying up to do so). Running on the cutting edge was the norm. Now... it's met with apprehension and reluctance. It comes and goes these days. There are times where I get absorbed in it (and love it) and there are times where I just don't want to mess with it... Maybe it's time for that cushy project manager's chair. That way the only thing I would have to keep current with is the level of the coffee pot and my MS Project files .

Oh.. yeah.... I havn't even opened the wrapper on Flex yet... Don't feel like messing with it .

DotNetTex