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

Slides:



Advertisements
Similar presentations
Microsoft® Office Word 2007 Training
Advertisements

Lecture 10 Sharing Resources. Basics of File Sharing The core component of any server is its ability to share files. In fact, the Server service in all.
Microsoft® Access® 2010 Training
Presented by: Benefits Systems Support. Getting Started ê Open Powerpoint, create a blank presentation. ê Select a style for your first slide from the.
Understanding Microsoft Excel
Microsoft ® Office Word 2007 Training Bullets, Numbers, and Lists ICT Staff Development presents:
Microsoft ® Office Word 2007 Training Table of Contents II: Customize your TOC [Your company name] presents:
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
Creating And Maintaining A Database. 2 Learn the guidelines for designing databases When designing a database, first try to think of all the fields of.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Academic Computing Services 2010 Microsoft ® Office Visio ® 2007 Training Get to know Visio.
Bullets, numbers, and lists Simple lists Lists are a useful component of any document, whether you’re summarizing information or making it easier to digest.
Advanced Tables Lesson 9. Objectives Creating a Custom Table When a table template doesn’t suit your needs, you can create a custom table in Design view.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Introduction to VB.NET Tonga Institute of Higher Education.
Power Point EDU 271 Microsoft PowerPoint is a powerful tool to create professional looking presentations and slide shows. PowerPoint allows you to construct.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
XP New Perspectives on Microsoft Access 2002 Tutorial 51 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and.
Adding Content To Your Faculty Page 1.Login 2.Create your Faculty Page 3.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
CIS—100 Chapter 15—Windows Vista 1. Parts of a Window 2.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Mail merge I: Use mail merge for mass mailings Perform a complete mail merge Now you’ll walk through the process of performing a mail merge by using the.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
InDesign CS3 Lesson 4 ( Only pages ) Importing and Editing Text.
‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7.
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
Namespaces Tonga Institute of Higher Education. Introduction to Namespaces The.Net Framework provides many classes for doing different things  File Input/Output.
XP New Perspectives on Microsoft Word 2002 Tutorial 31 Microsoft Word 2002 Tutorial 3 – Creating a Multiple-Page Report.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
XP New Perspectives on Microsoft Access 2002 Tutorial 21 Microsoft Access Tutorial 2 – Creating And Maintaining A Database.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Teaching a character to walk in more than one world: Parameters and Inheritance. By Lana Dyck under the direction of Professor Susan Rodger Duke University.
Visual Basic.NET BASICS Lesson 3 Events and Code.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Unit 1: Java and Eclipse The Eclipse Development Environment.
1 What to do before class starts??? Download the sample database from the k: drive to the u: drive or to your flash drive. The database is named “FormBelmont.accdb”
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Microsoft Access. Microsoft access is a database programs that allows you to store retrieve, analyze and print information. Companies use databases for.
Using Advanced Options Lesson 14 © 2014, John Wiley & Sons, Inc.Microsoft Official Academic Course, Microsoft Word Microsoft Word 2013.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Game Maker – Getting Started What is Game Maker?.
Geo 3 ch 5 Collecting Data Edited 10/14/05 1 GPS- Collecting Data Now that you have your data dictionary loaded, let’s configure the unit and take it outside!
The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can.
Fall 2003Sylnovie Merchant, Ph.D. ACCESS Tutorial Note: The purpose of this tutorial is to provide an introduction to some of the functions of ACCESS in.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
Learning PowerPoint Presenting your ideas as a slide show… …on the computer!
Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code.
Photoshop Actions Lights, Camera, Actions in Photoshop.
Build a database II: Create tables for a new Access database Create your tables In a relational database, tables store your data. Your data doesn’t “live”
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
Easy Site Navigation Getting organized, staying organized Creating custom site-navigation controls Using Menu, TreeView, and SiteMenuPath controls Make.
Word and the Writing Process. To create a document 1.On the Start menu, point to Programs, and then click Microsoft Word. A new document opens in Normal.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Troubleshooting Dashhawk Issues. Here's what you have to do to have the DashHawk run properly Go to the ACTUAL PROGRAM file (not the short cut)
© Paradigm Publishing, Inc.
Tutorial 3 – Creating a Multiple-Page Report
Microsoft Office Access 2003
Presentation transcript:

NAMESPACES AND VISUAL INHERITANCE

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

USING NAMESPACES

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.

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.

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?

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?

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?

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?

hh WHAT IS A NAMESPACE?

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

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

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

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

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

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

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

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

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

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

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.

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

MOVING THE GREATDANE CLASS

VISUAL INHERITANCE

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.

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.

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.