Terry Thibodeau's Blog

Welcome to Terry Thibodeau's Blog Sign in | Join | Help

Visual Studio *Magic* (code generation)

I know I'm not the only one who doesn't like *magical* code generation, so I thought I'd share this little tidbit of frustration I faced in case any of you out there run into the same issue.

I was recently working in an ASP.NET 2.0 app and when I tried to compile, I got errors about missing variables that I was using in the code file (yes, code file. Code behind is sooooo .NET 1.1). "Self,", I say to myself, "this is weird. My intellisense is working for the user control that it is complaining about. Why won't it compile?"

Now, I've mostly worked in WinForms apps, and haven't done anything significant in ASP.NET 1.1, so I hadn't run into this kind of issue before now. (As an aside, I'm running the Web Application Project add-in for VS2005, which I just found out is also included in VS service pack 1). It turns out that there were 3 or 4 things contributing to my issue.

The web project was messed up somehow. I had imported it from a previous solution, and somewhere along the way, things got weird. I noticed that when I created a brand new user control, there was the code file (usercontrol.ascx.cs) and a "designer" file (usercontrol.designer.ascx.cs).

Well, that's my problem! None of my other controls had the "designer" file. (I think what might have happened was that the original project was a Website project versus a WebApplication project, I don't know. All I know is this was annoying). So, off I go to recreate these "designer" files to let Visual Studio do it's magical code generation for me. Well, you can't just create the file with the right name, you also have to manually edit the project file and include the dependency on the "designer" file. Fine. I get the dependencies setup...all is good, or so I think.

I get the solution loaded up, see that there is now the "designer" file associated with all of my user controls and pages, but I still get complaints from the compiler that it can't find the controls I'm referencing. I look closer at the generated "designer" file code (yes, VS will generate the code if the file exists, but won't create the dependency...silly) and notice that there is no namespace declared. WHAT?!?!?!?? WHY?!?!?

I double, triple, quadruple check all of my dependency settings in the project and anything else I can think of. I carefully inspect and compare with a freshly created page the following line in my aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomUserControl.aspx.cs" Inherits="CustomUserControl" %>

I finally see the issue: The Inherits attribute doesn't include the namespace!!!! It should read:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomUserControl.aspx.cs" Inherits="Namespace.CustomUserControl" %>

I don't know where the namespace went in the first place, but after putting in the proper namespace, the "designer" file properly generates the namespace, user control variables, etc. and everything compiles nicely..

I know all of this isn't really a big issue once you've been through it once and understand all of the magical "help" that certain Visual Studio projects give you behind the scenes. But, the moment that something isn't EXACTLY how Visual Studio expects, the whole process collapses in on itself. That's not cool.

The moral of our story kids: Code generation *magic* sucks!

PS. One other thing to note is that the Web Application Project still uses "CodeBehind" which, if you look in the help is depreciated in favour of "CodeFile". I just thought that was amusing...

Published Tuesday, March 06, 2007 11:22 PM by Terry Thibodeau
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems