Inheriting Riches (inherited charm)

zhaozj2021-02-17  58

Inheriting ricles

Wayne S. Freeze

Welcome to VB.NEW, a monthly column dedicated to keeping you up-to-date with all that's new, different, and exciting for Visual Basic developers in the brave new .NET world. Whether you're just learning VB.NET or have Applications to migrate to the New Platform, VB.New Will Help You Und.New Features To Use. Wayne S. Freeze Offers The First Installment in This Regular Series.

Got the VB.NET blues? Then you need VB.NEW! VB.NEW is a column devoted to exploring the new features found in VB.NET. Over the coming months, I'll discuss topics such as overloading, namespaces, assemblies, new classes, new statements, and lots more to help you use VB.NET effectively In this first column, I'm going to focus on what's probably the biggest single change to Visual Basic since Basic was updated to run under Windows:. inheritance.

Object-oriented programming involves a different way of thinking. In the past, you created objects by creating a Class module. You had to explicitly define every property, method, and event. Many programmers exploited this capability to create classes that were merely collections of related functions without any true organization. While this is a pretty good technique, it is not truly object-oriented programming. In order to do object-oriented programming, you must have inheritance. But what, exactly, is inheritance?

Inheritance is like creating a new class by copying an existing class. But it's a special kind of copying. With inheritance, you do not simply duplicate all of the original's stuff in the copy. Instead, the copy (or child) has references that point back to the original (or parent's) properties and methods. This is important, because this way, if the parent is changed, the child is automatically updated with those changes.Inheritance makes reuse of existing code easy, since your new class automatically includes All of the Properties, Methods, And Events from The Parent Class. Then You CAN Add, Replace, or Modify The Properties, Methods, And Events with your owmb code, while leaving the original code intact.

Classy programmingA Class module is just a template that's used to create an object. It defines the various properties, methods, and events that are associated with the object. If you've used Class modules in VB6, all of the same concepts apply to VB .NET. HOWEVER, THERE HAVE The SYNTAX. SONTAX. SONTAX. Some of these Changes Are Just Different Ways of Doint To Implement Inheritance and Other New Features.

One key difference between classes in VB6 and in VB.NET is the concept of a constructor. A constructor is similar to the Class_Initialize event in VB6 in that it often contains initialization code that's called when a new instance of an object is created.

You might wonder whether there's a corresponding destructor routine, which would clean up after the object is no longer needed. While many object-oriented programming languages ​​include destructors, they do not exist in VB.NET. The Common Language Runtime (CLR) includes a tool known at the Garbage Collector, which will automatically detect and destroy any objects that you create and no longer use.Inheritance hierarchiesThe trick to designing object-oriented programs is to think in terms of hierarchies. Each object is inherited from a higher-level object that is itself, in turn, inherited from a still higher-level object. The root class is the most general, and as you move down the hierarchy, the objects become more specific. VB.NET encourages this by making it easy to take Microsoft-supplied classes and inherit from them to create customized children as you see fit. VB6 used a simplified version of this concept, by allowing you to create user controls by using existing controls. However, you still had to write a lot of code to make everything work properly. You had to define the properties and methods that you needed and include code that passed the request on to the base control for execution.

With true inheritance, you do not need the code to pass along the information to the base object. Your object automatically uses the code in the base object unless you explicitly override it. This means that you have to write less code, which translates into Fewer Places for Bugs in your code.

An inheritance exampleIn order to better understand inheritance, let's go through a simple example Assume that you have the following class, which is called Person This class consists of two properties, FirstName and LastName, and one method, FullName:.. Public Class Person Public Firstname as string public lastname as string public function fullunname () as string fullname = firstname & "& lastname end function)

Now suppose you want to create another class called Employee, with the properties FirstName and LastName used in Person, plus a new property called EmployeeNumber. And you still want to be able to use that FullName function. You can type in the three property definitions and Copy The Function, Which Duplicates Work You've Already Completed, or You Can Inherit The Person Class and Add The New Property As Shown In The Following Class Definition:

Public Class Employee Inherits Person Public Employeenumber AS Integer End Class

That's all there is to it !ow Employee Has All of The Properties and Methods of Person, Plus Its Own Employeenumber Property.

Overriding method is the Ability to replace a method in the base class with one thing.

Let's add another class called Customer to the example. This class, too, will inherit from Person, just as Employee did. It will add a CustomerNumber property. In addition, the FullName method should append the customer number at the end of the customer's name . The class tria Follows How To inherit The Person Class and Override The Fullname Method with a New Method:

Public Class Customer Inherits Person Public CustomerNumber As Integer Public Overrides Function FullName () As String FullName = MyBase.FullName & _ "(" & CStr (CustomerNumber) & ")" End Function End ClassThe first thing you should notice with this class is that the function definition includes the Overrides keyword. This keyword means that the function replaces the corresponding function in the base class. The next thing is that there's a reference to MyBase in the body of the function. This is how you reference properties and methods in the base class even though they have the same name in the current class. Thus you can continue to use Person's FullName method to put the names together and simply append CustomerNumber to the end of the result.

In order to use the Overrides keyword, the corresponding subroutine or function must be marked as Overridable in the base class Thus, the FullName function definition in the Person class would need to look like this.:

Public overridable function fulname () AS STRING

Other classic optionsIn addition to the Overridable and Overrides keywords, there are two other keywords that control how you can override a method. The first is NotOverridable, which means that the method can not be overridden in an inherited class. The second is MustOverride, Which Means That The Method Must Be Overridden in The inhrited class.

When you declare a class, you can specify a set of modifiers that describe how the class can be used. By default, any class can be inherited by another class. You can prevent this by preceding the Class keyword with NotInheritable.

Likewise, you can prevent someone from using the class directly by using the MustInherit keyword. MustInherit works well with the MustOverride keyword on particular methods in the class. This is useful if you develop a general-purpose class that you intend for other developers to inherit and extend by replacing certain methods with new ones that are specific to the inherited class.The Class, Sub, and Function statements also include keywords that control the visibility of that particular definition. While these keywords do not affect inheritance directly, you should be Familiar with the visibility of the ity Control The Visibility of The Item with Which they're associated.

Public and Private control whether the class or method is visible outside the item's current scope. A Private method is visible only inside the current class, while a Private class is visible only to the current module. Of course, if the current class is nested inside Another Class, It Will Be Visible Only To The Outer Class.

While VB6 supported the Friend keyword, it was not used a lot. Its visibility is somewhere between Public and Private. For code outside the current project, the class or method is considered Private, while for code inside the current project, the class or Method is considered public.

VB.NET introduces a new keyword called Protected, which marks a method as visible to an inherited class. This means you can create methods that can only be used within the inherited class. If you merely created an instance of that class, then the methods Would Be Hidden. this is a great approach if you want to provide access to helper routines you buy in the base class to someone Who Wants To Derive A New Class from it.

You can use the Protected Friend keyword to limit the access of Protected methods to the current project. This is useful if you have a set of methods you need to use within the project, yet you do not want people using them from outside the project .The root of all objectsIn VB.NET, everything is treated like an object. This includes not only things like forms and controls (which you might expect), but also variables. The Object data type has replaced the Variant data type as the default data type in Visual Basic. All classes defined in the .NET architecture are inherited either directly or indirectly from the Object data type. Understanding inheritance is critical to writing effective VB.NET applications.

While inheritance is a great concept

One of the classic problems with designing applications in Visual Basic is the amount of work needed to present a common look to all of the forms in an application. Generally, a VB6 developer will create a form that can be loaded into a project or used as A Template. The Form Includes The Menu Items and Controls Necessary To Present A CommON Look and Feel Across The Application.

However, this approach means that the form is frozen when it's incorporated into a program. Any changes made to the form template after the form was loaded must be manually made to each specific instance of the form. This can be a very serious problem if your Application Uses a Lot of Standard Forms.

In VB.NET, forms are now a class that can be inherited just like any other class. Thus, you can easily create a standard form object that can be shared by all of the programs in an application. Each program is free to add controls to the form or modify the controls already on the form. However, the code associated with the base form is independent of the code used in the program. This allows the base form to be updated as necessary, and the updated code will automatically be incorporated into any form that inherits the base form. Microsoft refers to this technique as visual inheritance.Another example of how inheritance might be used in your application comes from how errors are handled in VB.NET. The Exception class contains basic error information about an error That Occurred in Your Program. Its IT IT Message Associated as The TEXT Message Associated with the error, a line to a help file, And a stack trace tror ips identify where the error occurred.

However, other classes frequently inherit the Exception class so that they may include additional information about an error. For example, the ADOException is indirectly derived from the Exception class and contains additional information such as the specific error code associated with the error and a reference to An AdoerRors Object, Which Documents Other Errors That Occurred During The Last Database Request.

Having specific classes for errors makes it easier to handle error conditions in VB.NET, which is the topic for my next column. In the meantime, if you have questions or comments, please feel to send them to me at WFreeze@JustPC.com I'D love to hear from you!

转载请注明原文地址:https://www.9cbs.com/read-30876.html

New Post(0)