Amanda Silver - Demonstration of code separation in next version of Visual Basic
- Posted: Oct 29, 2004 at 2:14 PM
- 57,345 Views
- 11 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
The thing I've found the hardest to deal with, is HTML designers--they don't like it when we render HTML code in the code behind, so we're forced to put some of the rendering code in the display portion of the ASPX page. If there was only a way to switch them to the other side of the force...
But, to return to the main point, Amanda showed me how the mechanism works, but not how it will HELP ME. What does it buy ME?
In addition, having your code spread out like that, would that not make maintenance more difficult? Or did I misunderstand partial class altogether?
Basically, if you're generating code, you're going to want to have some way for the programmer to extend the generated code. And you're going to want to do that in a way that lets the programmer treat the generated code as a black box, and most importantly in such a way that regenerating the code will not overwrite your programmer's code. As anyone who's dealt with code generation can tell you, hand-crafted code is holy!
There are several possible solutions.
You can have the generated code in the same file as your code. This is what Visual Studio does in the current version. The problems with it is that it messes up the file. It becomes harder to read your code because of the designer-generated code that's in between it.
You can have the generator create some class, and then you inherit from it. And you can have the generator create some class that inherits from you. Both methods have the disadvantage that you're creating two classes that describe usually just one entity, and where the base class makes no sense without the child class. So you're making a separation where there should not be one logically.
ASP.NET actually uses that last one with code-behind files (I'm not talking about Visual Studio now, just ASP.NET itself). You write a code-behind class, ASP.NET generates one from the aspx file that inherits from your class. This happens the first time a page is accessed by a browser.
Now anyone who has written aspx files with code-behind without using the VS.NET designer knows this has a very big problem. For every <SomeControl ID="SomeID" Runat="server"> in your aspx file, you need a Protected SomeID As SomeControl in your code-behind file. If you're using the designer, it makes sure your code-behind declarations and aspx controls keep in synch. But if you don't like the designer, and like me write aspx code by hand, it's up to you to keep those declarations correct.
The code-beside model (partial classes) fixes that. Now the ASP.NET runtime creates a partial class from the aspx file. Because these merge into one logical unit, any declarations that the ASP.NET runtime generates are automatically visible in your partial classes, so there's no need for repeat declarations.
For the WinForms designer, the advantage is that your own code file is less cluttered with designer-created code.
I can't offhand think of a situation where it'd be practical to write partial classes by hand, but there probably are some anyway.
It will be more usefull in Longhorn and Avalon sepecificaly because you can define you UI part of your class in a XAML file and the code in the other part of the class in a CS file (in C# for exemple).
.
But I'm sure programmers will find other creative ways of using it
In fact, I'm working on a code generation system myself, and I'm wishing I could use those partial classes. But since I'm targetting .Net 1.1, I can't do that.
For stuff like the ASP.NET runtime, the XAML compiler, etc. partial classes is a much needed, and from my developer point of view, much welcomed, feature.
For stuff like the Windows Forms designer, the need is less there, mainly because of #region, but it's still a cleaner separation of designer-generated code and hand-crafted code than what's done now, while avoiding the trouble associated with an inheritance based solution.
I don't think it'll be so useful for manual coding. Separating hand-crafted code over many files may lead to maintenance problems. And I think that if you have a class that's so big it warrants spreading over more than one file, you probably need to rethink your design.
Also, they've not added "everything they can think of" yet. Don't forget that VB.NET 2005 is also getting generics, unsigned types (way overdue) and operator overloading. Those are also much welcomed features.
I agree with what Sven Groot has mentioned. Intensive usage of these partial classes may lead to some maintenance issues too. And also, it could as well be a design aspect when you need to write a huge code in a single class. But this concept sounds to be more useful in the auto code generation environments.
Not trying to be a Troll, but I'm trying to see the advantage of using a Partial Class over an Object Hierarchy.
I don't necessarily buy the "It's Better for Code Generation" argument as that can be accomplished the using either method.
Brien
I worked on a project (VB6/ASP) one time (remember I didn't write it, I just had to maintain it) which had a DLL with one class in it. This one class in the DLL contained every function/method that was used throughout the application. The purpose of this DLL was to be a layer between the ASP page and the Database. When I maintained this app, I was working with 3 other programmers and we had to coordinate changes to this VB6 Class that needed to made all at the same time. Even though this is a really bad practice and you should never design an app this way, this is still a prime candidate for misuse of the Partial class functionality. I could see a programmer that has very little design knowledge seeing Partial classes as a perfect solution to break up that one class into multiple code files that each contained a certain set of functions/methods.
I know there is no way for the VB Team to really solve this, since it's all the programmers responsibility to learn how to correctly create classes. I just wanted to bring this up since this is a perfect example of a really bad misuse of this functionality.
Remove this comment
Remove this thread
close