Presentation is loading. Please wait.

Presentation is loading. Please wait.

NAMESPACES AND VISUAL INHERITANCE. OBJECTIVES In this chapter, I will cover the following: Using namespaces Visual inheritance.

Similar presentations


Presentation on theme: "NAMESPACES AND VISUAL INHERITANCE. OBJECTIVES In this chapter, I will cover the following: Using namespaces Visual inheritance."— Presentation transcript:

1 NAMESPACES AND VISUAL INHERITANCE

2 OBJECTIVES In this chapter, I will cover the following: Using namespaces Visual inheritance

3 USING NAMESPACES

4 INTRODUCTION Namespaces allow you to organize a large number of classes into a hierarchical structure, which promotes code reuse and inheritance among classes. Without namespaces, a project quickly becomes cluttered with classes and other objects that are all on the same root namespace (the project name). Moving classes into namespaces that grow out of your own custom root namespace allows you to keep classes separate.

5 WHAT IS A NAMESPACE? A namespace is a hierarchical structure for organizing classes. You can create and use any namespace name that you want in your programs, as long as it doesn’t interfere with any other namespaces that use the same name. Technically, you can add your own classes to existing namespaces. For example, you can add your own classes to the System or Microsoft namespaces, but I recommend against it. The flexibility is nice, but you are going to make better use of a namespace by using one of your own design.

6 The entire Microsoft.NET Framework is a huge collection of classes organized using namespaces. You have already used many of these built-in namespaces, such as System.Windows.Forms.Form and System.Math. In those cases, System is the root namespace, where as Windows.Forms.Form and Math are namespaces that branch off System. WHAT IS A NAMESPACE?

7 If you recall, the Animal program includes a bunch of classes for the animals in a pet shop, and these classes were just added to the end of the source code for Form1. The goal of this chapter is to organize the Animal classes, while demonstrating how to create a namespace. So, let’s get started on that right now. First, this program needs some breathing room, so let’s add a new file and start moving those classes out of Form1. WHAT IS A NAMESPACE?

8 Add a new class and name it Pet.vb. Okay, now you get to create the new namespace! It’s really easy, only involving a couple lines of code. You will be surprised at how easy and flexible the namespace feature of VB.NET really is. Add a couple blank lines above the Public Class Pet line, and type in Namespace Pets. But don’t press the Enter key after typing it in! If you do that, VB.NET will automatically add the End Namespace line, and it really needs to go below the Pet class. WHAT IS A NAMESPACE?

9 It is surprising how much VB.NET does automatically. But instead of pressing the Enter key, press the down arrow key, and watch what happens. VB.NET indents the Pet class statement! You might have noticed the blue squiggly line below the Namespace Pets line. This is another amazing feature of VB.NET; it compiles your program as you type, immediately finding bugs for you. Take a look at the following figure for what do do. WHAT IS A NAMESPACE?

10 hh WHAT IS A NAMESPACE?

11 Now that you have a new namespace, let’s finish the Pet class. The Pet class is going to inherit from the Animal class that you created in the previous chapter. COMPLETING THE PET CLASS

12 Now that you have a nice start on the Pets namespace, let’s start moving code out of Form1 and into the namespace. Open the source code window for Form1, and locate the Animal class, starting with the line that shows Public Class Animal. Cut all the Animal Class from Form1 source code. Next, open the Pet.vb file again, move the cursor below the Namespace Pets line, and paste the text. MOVING THE ANIMAL CLASS

13 If you shrink the blocks of code for both the Animal and Pet classes, using the code shrink/expand symbols (which resemble the + and - symbols in Windows Explorer), the result should look like MOVING THE ANIMAL CLASS

14 The next thing I’m going to do is likely to sound confusing, but with a snap of the finger, it will all make sense. What I want to do is add a level, Dogs, to the Pets namespace, and then move the Dog classes into this namespace. Also, the main Dog class will be moved into the root Pets namespace. Add a new class just like you did earlier to add the Pet.vb file. Make sure you select the Class template, and name the new file Dog.vb. CREATING THE Pets.Dogs NAMESPACE

15 First, add Namespace Pets around the Dog class, as you did earlier with the Pet class. Didn’t you already create the Pets namespace in the previous step? Yes, you did! That’s the magical part of namespaces. You really can do anything you want with a namespace. As I have mentioned already, namespaces are organizing structures and are not really functional as far as solving problems. CREATING THE Pets.Dogs NAMESPACE

16 You can add classes to multiple files in a project that all share the same namespace. By surrounding classes with the Namespace name block, those classes are part of the namespace. You can also rename the namespace at any time; just be mindful that doing so may break existing code. Because classes are inside a namespace, you must provide the namespace name as well as the class name when using those classes. CREATING THE Pets.Dogs NAMESPACE

17 For example, to use the Pet class created earlier, you would create a variable like this: Dim pet1 As New Pets.Pet See how the namespace and dot operator are added to the front of the class name, Pet? IntelliSense works here too, so you can see your entire namespace structure as you type in the variable declaration. CREATING THE Pets.Dogs NAMESPACE

18 Okay, now let’s add the Pets and Pets.Dogs namespaces to this file. You already have the new Dog.vb file open, so just add the namespace blocks as shown in the following figure: MOVING THE DOG CLASS

19 The GreatDane class is going to be moved into the Pets.Dogs namespace, whereas the main Dog class is going to be moved into the Pets namespace (in this file, at least). First, let’s work on Dog. Cut the Dog class out of the Form1 file, and paste it into the Dog.vb file. The result should look like: MOVING THE DOG CLASS

20

21 Lets now make some few changes: make a single change to the Dog class so that it inherits from Pet instead of Animal. Next, add two lines to the constructor: ‘new property from Pet Food = “doggie chow” The result should be as follow (the changes have been highlighted for more visibility): Why are all these changes needed, you might ask? The new Pet class that was added to the project inherits from Animal, so none of the pets will inherit directly from Animal any longer. MOVING THE DOG CLASS

22

23 Instead, they will inherit from Pet. As a result, the animals need to use a new property of the Pet class: Food. Until now, the pets have all been eating the same generic chow, but we want to give each animal their favourite kind of food. Fortunately, this change only needs to be made to the base classes (such as Dog), not all the sub-classes. I have deleted the Console.WriteLine lines because they were just helpers in the previous chapter and are no longer needed.

24 MOVING THE GREATDANE CLASS You won’t have to make any changes at all to GreatDane or MiniDoxen. But isn’t it great just cutting and pasting code, rather than typing it all in? In the real world, this is something that you will often have to deal with. Open the Form1.vb file again and cut the GreatDane and MiniDoxen classes out of it. Then paste the classes into the Dog.vb file inside the Pets.Dogs namespace (at the bottom of the source code).

25 MOVING THE GREATDANE CLASS

26 VISUAL INHERITANCE

27 INTRODUCTION Visual inheritance is a new feature in VB.NET, which allows you to reuse forms and controls between projects. Ideally, you would compile a form or control as a component into a.DLL file, so it could be shared. For demonstration purposes, I’ll just show you how to inherit one form into another, within the same project.

28 CREATING A REUSABLE FORM Create a new Windows Application called VisualInheritance. Add four controls to the default Form1: a Label, a TextBox, and two Button controls. Now, double-click the first button to open up the default event for the button and type the following code: If TextBox1.Text.Length > 0 Then MsgBox(“Hello, “ & TextBox1.Text & “!”) End If Likewise, double-click the second button and type the following code in Button2_Click: End Now run the program.

29 REUSING THE REUSABLE FORM Now, creating inherited forms is fairly easy, because VB.NET has a menu item that does all the work for you. And actually, you can do it yourself because the code that VB.NET generates is extremely short, as you’ll see. Select Project, Add Inherited Form. The Add New Item dialog appears, with the Inherited Form template already selected. Accept the default name of Form2.vb and click the Open button.


Download ppt "NAMESPACES AND VISUAL INHERITANCE. OBJECTIVES In this chapter, I will cover the following: Using namespaces Visual inheritance."

Similar presentations


Ads by Google