Partial Success

Thursday, June 24, 2004

I have a problem when new keywords appear in a language. I consider the problem odd, because I am generally fond of new features. XAML? Super! One hundred new classes in the base class library? Excellent! Hosting the CLR inside of SQL Server? Positively orgasmic! Yet when a new keyword appears in my development language, I respond with complete indifference.

I first recognized this strange behavior when the mutable keyword appeared in C++. I’d be reading an article on C++ and suddenly mutable would be mentioned. My eyes would skip over the section, just like they do when I run across an advertisement for something I don’t really need, like a maternity dress.

Now today, I performed an experiment with the May CTP of VS 2005. Ever since the release of Visual Studio.NET people have been designing base classes for their Web forms to inherit from. In the first design iteration these classes will be marked as abstract (must inherit), but the VS.NET designer does not like this one bit, and will not let you edit the form in code mangling mode (also known as “design mode”). I just wanted to see if this was still the case.

After starting a new web project, I switched to the code view and came up with this:

public abstract class MyBase : System.Web.UI.Page
{
}
 
public partial class Default_aspx : MyBase 
{
}

To which the compiler gleefully responded:

"Build (web) : Partial declarations of 'Default_aspx' must not specify different base classes"

Partial? WTF is partial? I’d seen it mentioned in various blogs hither and thither, but to me it was still a maternity dress. All of a sudden, here it was on my screen. It was talking to me. It was saying:

I’ve got your attention now, loser.

So I did some reading. Obviously, the ASPX file had a different intention as to who it wants to inherit from. I prepared for a battle of wills. I remembered the Inherits attribute in the @Page declaration and tried this:

]]>

And the compiler responded with a laugh:

"Build (web) : Could not load type 'MyBase'." 

Partial: 2 Me: 0

For my next offensive I launched Google, and found Matt Bether’s blog. Matt had pulled this off without a problem, so I didn’t lose hope. I thought I just had to pay my penance for snubbing partial.

Matt's base class wasn't abstract, so I was beginning to wonder if the abstract base was causing more problems. I tried removing the abstract keyword from MyBase - but still no go. Next, I made a guess that something looks at the @Page directive, sees the Inherits tag, and tries to load the type to make sure it is a type compatible for a webform. I’m not sure why, but I think having the base class type defined in the same/almost same compilation unit as part of the webform class causes difficulty. Can anyone shine some light on this? I moved the MyBase class into a separate new file (MyBase.cs) in the magic Code directory. Voila! No errors. Even better news: I can load the form into design view without a designer error, which was all I had wanted to test in the first place.

At this point I’m not sure I like the fact that I have to specify the base class in the @Page directive. I’d be much more comfortable if the partial class in the code view would allow me to specify the base class. It feels much more natural and is easier to see. I hope it changes. I see Natty Gur has a similar request.

If I chalk up the experience as a partial success, that would sound partially lame. Ok, completely lame, but it’s late, and I’m about to hit the POST button.


Comments
Christian Romney Friday, July 16, 2004
Worse is trying to inherit from a class in another assembly. you'll have to create a class in the Code directory which inherits from the external class and then make your web form inherit from that. Sucks.
George Hayes Saturday, March 19, 2005
You might find this simple approach which I just figured out, to be a good solution to removing the annoyance...
<br>
<br>Regards,
<br>George
<br>
<br>(#)define a directive constant DesignMode, then
<br>declare your abstract base class thus:
<br>
<br>#if DesignMode
<br> public class TestBase : System.Web.UI.Page
<br>#else
<br> public abstract class TestBase : System.Web.UI.Page
<br>#endif
<br>{
<br> Class stuff...
<br>}
<br>
George Hayes Saturday, March 19, 2005
My apologies,
<br>In my enthusiasm, I left out the other half of the solution:
<br>
<br>Declare your abstract function similarly:
<br>
<br>#if DesignMode
<br> protected virtual void LoadPage(){}
<br>#else
<br> protected abstract void LoadPage();
<br>#endif
<br>
Scott Saturday, March 19, 2005
Nice tip, George
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!