Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG

Reader rating:
Click here to read 6 comments about this article.
Article source: CoDe (2001 - Issue 2)


Article Pages:  1  2 3 4 5 - Next >


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.

Click for a larger version of this image.

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).

Click for a larger version of this image.

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.



Article Pages:  1  2 3 4 5 - Next Page: 'Understanding Visual Inheritance in .NET (con't)' >>

Page 1: Understanding Visual Inheritance in .NET
Page 2: Understanding Visual Inheritance in .NET (con't)
Page 3: Understanding Visual Inheritance in .NET (con't)
Page 4: Adding new behavior in C#
Page 5: Virtual Methods

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.7 out of 5

41 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      AppsWorld Europe

 

SSWUG