Understanding Visual Inheritance in .NET Inheritance is the single most important new object-oriented feature in Visual Studio.NET. Surprisingly, not much has been written about the subject, and most of the information available is either very basic and an "overview" at best, or just plain misleading. In this article, I give you a real-world overview of what inheritance ? especially visual inheritance ? can do for you. Inheritance is a key concept for a language or environment to be considered truly object-oriented, rather than just object-based. In previous versions of Visual Studio (up to version 6.0), Microsoft made inheritance available in languages such as Visual C++, Visual FoxPro and Visual J++, but not in Visual Basic. This means that the majority of Microsoft's developer community is in for a major paradigm shift. This article will help make the transition as smooth as possible for you. Unlike the descriptions in most .NET books and articles, inheritance is relatively simple. I recently read several articles that make statements like, "Free Threading and Inheritance are powerful features, but a lot of Visual Basic developers will shoot themselves in the foot applying these technologies incorrectly." While I agree with this statement for the Free Threading feature, I have to disagree with the inheritance part. Visual Basic developers who are already familiar with object-based development face a steep, but fairly short learning curve. C++ and Visual FoxPro developers moving into VB.NET or C# will be able to make this move painlessly. In this article, we will create a base data entry form class that provides some standard features. We will subclass this class to create individual data entry forms. Beyond that, we will create specialized subclasses of the subclassed data entry forms. We will create all examples in Visual Basic.NET and C#. Inheritance is a feature provided by the Common Language Runtime (CLR) and is, therefore, not language specific. As you will see, all the concepts are available in a similar fashion in both languages, although the syntax is slightly different. All our examples will have methods that would in real life normally provide a lot of functionality (such as saving information). However, we will focus our attention on the Inheritance part of this, and will simply represent all other code with message boxes, so we can easily see what's going on in our code. Creating a Base Data Entry Form Class The basic scenario is simple: We want to create a base data entry form class that provides OK, Cancel and Apply buttons. Those buttons will trigger Save() and Cancel() methods. The base class itself is not designed to be used for data entry ? we will use subclasses (classes based on the base class) for that. Visual Basic developers can imagine the base form like a "template on steroids." Figure 1 shows what our base data entry form class looks like.  Figure 1 Our data entry base class is rather simple. All other details will be added in subclasses.To create this class, create a new WinForms project (C# or VB.NET). This will automatically create a default form for you. You could use this form and turn it into our base form, but I used mine only as a test-bench to test our classes. To add the real class, right-click the solution in the Solution Explorer (I named my solutions CSInheritanceSample and VBInheritanceSample) and select "Add Windows Form." As the name, use "DataEntryBaseForm". Add three new buttons to the form (as shown in Figure 1) and set the names to "cmdOK", "cmdCancel" and "cmdApply". To make our form always look nice, we would like the buttons to maintain their position relative to the bottom-right edge of the form. In Visual Studio 6 languages (pretty much all of them), we would have needed to write manual resize code. In Visual Studio.NET, we can simply set the Anchor property to "BottomRight" (see Figure 2).  Figure 2 We create the base form and define the buttons to be positioned relative to the bottom right corner of the form.In addition, we add two methods to the form: Cancel() and Save(). The buttons on the form will call those methods. In this example, those methods don't do anything by default but show a message box. The idea is that we could later add code to these methods in the base class to automatically provide inherited save and cancel functionality in the subclasses. Or, we could override or augment the base class methods by adding code in the subclasses. But, I'm getting ahead of myself. Let's look at the base form class code first. Here is the C# version: namespace CSInheritanceSample { using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.WinForms;
public class DataEntryBaseForm : System.WinForms.Form { private System.ComponentModel.Container components; private System.WinForms.Button cmdOK; private System.WinForms.Button cmdCancel; private System.WinForms.Button cmdApply;
public DataEntryBaseForm() { InitializeComponent(); }
public override void Dispose() { base.Dispose(); components.Dispose(); }
private void InitializeComponent() { this.components = new System.ComponentModel.Container (); this.cmdOK = new System.WinForms.Button (); this.cmdApply = new System.WinForms.Button (); this.cmdCancel = new System.WinForms.Button (); cmdOK.Location = new System.Drawing.Point (120, 185); cmdOK.Size = new System.Drawing.Size (60, 23); cmdOK.TabIndex = 2; cmdOK.Anchor = System.WinForms.AnchorStyles.BottomRight; cmdOK.Text = "&OK"; cmdOK.Click += new System.EventHandler (this.cmdOK_Click); cmdApply.Location = new System.Drawing.Point (248, 185); cmdApply.Size = new System.Drawing.Size (60, 23); cmdApply.TabIndex = 0; cmdApply.Anchor = System.WinForms.AnchorStyles.BottomRight; cmdApply.Text = "&Apply"; cmdApply.Click += new System.EventHandler (this.cmdApply_Click); cmdCancel.Location = new System.Drawing.Point (184, 185); cmdCancel.Size = new System.Drawing.Size (60, 23); cmdCancel.TabIndex = 1; cmdCancel.Anchor = System.WinForms.AnchorStyles.BottomRight; cmdCancel.Text = "&Cancel"; cmdCancel.Click += new System.EventHandler (this.cmdCancel_Click); this.Text = "DataEntryBaseForm"; this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); this.ClientSize = new System.Drawing.Size (312, 213); this.Controls.Add (this.cmdOK); this.Controls.Add (this.cmdCancel); this.Controls.Add (this.cmdApply); }
protected void cmdApply_Click (object sender, System.EventArgs e) { this.Save(); cmdApply.Enabled=false; }
protected void cmdCancel_Click (object sender, System.EventArgs e) { this.Cancel(); this.Close(); }
protected void cmdOK_Click (object sender, System.EventArgs e) { this.Save(); this.Close(); }
public void Save() { MessageBox.Show("Base Form Save"); }
public void Cancel() { MessageBox.Show("Base Form Cancel"); } } }
| & | | 
By: Markus Egger Markus is the founder and publisher of CODE Magazine and EPS' President and Chief Software Architect. He is also a Microsoft RD (Regional Director) and the one of the longest (if not THE longest) running Microsoft MVPs (Most Valuable Professionals). Markus is also a renowned speaker and author.
Markus' spends most of his time writing production code. The projects Markus has worked on include efforts for some of the world's largest companies including many Fortune 500 companies. Markus has also worked as a contractor for Microsoft (including the Visual Studio team). Markus has presented at many industry events, ranging from local user groups to major events such as MS TechEd. Markus' written work has been published extensively and in magazine ranging from MSDN Magazine, to Visual Studio Magazine, and of course in Markus' own CODE Magazine and much more. Markus is a supporter of communities in North America, Europe, and sometimes even beyond.
Markus currently focuses on development in .NET (Windows, Web, Windows Phone, and WinRT) as well as Android and iOS. He is passionate about overall application architecture, SOA, user interfaces and general development productivity and building maintainable and reusable systems.
In his spare time, Markus is an avid windsurfer, scuba diver, ice hockey player and world traveler. On a rainy day, he is known to enjoy a good game on his PC or Xbox.
megger@eps-software.com | Fast Facts | | Cross-language inheritance is one of the major new features in Visual Studio.NET. It is extremely powerful and rather straightforward. | |
|