Tải bản đầy đủ (.pdf) (6 trang)

Visual studio 2010 part 8 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (126.46 KB, 6 trang )

67
Chapter 3
Learning Just Enough
C# and VB.NET:
Types and Members
68 Microsoft Visual Studio 2010: A Beginner’s Guide
Key Skills & Concepts
● Create Classes
● Write Methods
● Code Fields and Properties
A
type is a general term for classes, modules, enums, and more. This chapter will
specifically discuss the class type, which allows you to create your own custom types.
You’ll also see the value of a class when you learn about class members. You’ll see how
the field, method, and property class members can be used. We’ll start with learning how
to create and use classes.
Creating Classes
Previously, you learned about the primitive types, which are built into languages and alias
the underlying .NET types. You can also create your own types, via classes, which you
can instantiate and create objects with. The following section explains how to create
a class and then instantiate an object from it.
Class Syntax
To create a new custom class definition, right-click the project, select Add | Class, name
the class Employee for this example, and type the file extension .cs for C# or .vb for VB;
then click Add (VS will add this file extension for you if you don’t). You’ll see a file with
the same code as Listing 3-1.
Listing 3-1 A new Employee class
C#:
using System;
using System.Collections.Generic;
using System.Linq;


using System.Text;

namespace FirstProgram
Chapter 3: Learning Just Enough C# and VB.NET: Types and Members 69
{
public class Employee
{
public string FirstName;
}
}
VB:
Public Class Employee
Public Dim FirstName As String
End Class
The C# Employee class is nearly the same as the Program class that you created in the
preceding chapter, except that the class name here is Employee. In VB, you’ve only created
a module before, and the Employee class is your first class for this book. You can add
members to a class, which could be events, fields, methods, and properties. Listing 3-1 shows
an example of a field, FirstName, and you’ll learn about events, methods, and properties in
later sections of this chapter. A field is a variable in a class that holds information specific to
that class.
Listing 3-2 shows how to instantiate an object of type Employee, which is your new
custom type, and use it. You would put this code inside of Main or another method. You’ll
learn more about methods in the later section “Writing Methods.”
Listing 3-2 Code that uses a class
C#:
Employee emp = new Employee();
emp.FirstName = "Joe";
VB:
Dim emp As New Employee

emp.FirstName = "Joe"
In Listing 3-2, you can see that emp is a variable declared as type Employee. The C#
new Employee() or VB New Employee clause creates a new instance of Employee, and you
can see that this new instance is being assigned to emp. With that new instance, via the emp
variable, you can access the Employee object, including its instance members. In Listing 3-2,
the FirstName field of that particular instance of Employee is assigned a string value of "Joe".
Here you see that an object can contain data.
70 Microsoft Visual Studio 2010: A Beginner’s Guide
Now that you can define a new class, create an instance from that class, and use it, the
next section shows you another feature of classes called inheritance.
Class Inheritance
One class can reuse the members of another through a feature known as inheritance. In
programming terms, we say a child class can derive from a parent class and that child
class will inherit members (such as fields and methods) of the parent class that the parent
class allows to be inherited. The following example will create a Cashier class that
derives from the Employee class. To create this class, right-click the project, select Add |
Class, and name the class Cashier. Listing 3-3 shows the new class and modifications for
implementing inheritance.
Listing 3-3 Class inheritance
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FirstProgram
{
public class Cashier : Employee
{
}

}
VB:
Public Class Cashier
Inherits Employee

End Class
The C# inheritance relationship is indicated by the colon after the Cashier identifier,
followed by the class being derived from, Employee. In VB, you write the keyword
Inherits, on a new line, followed by the class being derived from. Essentially, this means
that Cashier has all of the same members as Employee. Listing 3-4 demonstrates the
benefits of inheritance.
Chapter 3: Learning Just Enough C# and VB.NET: Types and Members 71
Listing 3-4 Code using inheritance
C#:
Cashier cashr = new Cashier();
cashr.FirstName = "May";
VB:
Dim cashr As New Cashier
cashr.FirstName = "May"
According to Listing 3-4, Cashier does not have a field named FirstName. However,
Employee does have a FirstName field and Cashier derives from Employee. Because
of inheritance, Cashier automatically inherits FirstName, and the code in Listing 3-4 is
perfectly legal. Inheritance can be thought of as specialization in the sense that, in this
example, Cashier is a specialized kind of Employee. To take advantage of this specialization,
you could add a new field to your new Cashier class called “assignedCashRegister” where
now, not only does the Cashier class have the fields and methods of Employee, it is able to
hold the value for a specific cash register name or number. An instance of the Employee
class would not be able to contain this information. The .NET Framework uses inheritance
extensively to offer you reusable class libraries.
TIP

You can often use the phrase “is a” to describe the relationship between inherited
classes when starting from the child class. For example, you can say “Cashier is an
Employee.” If you apply this phrase technique to your software design and the sentence
sounds logically correct, then you’ve probably used inheritance correctly.
The class Snippet
C# has a class snippet, but VB doesn’t. Before using the class snippet, create a new class
file by right-clicking the project, select Add | New Item | Code File, and name the file
Manager. You’ll now have a blank file to work with. To use the class snippet, type cl and
press
TAB, TAB; and you’ll see the snippet template in Figure 3-1.
Figure 3-1 The C# class snippet template
72 Microsoft Visual Studio 2010: A Beginner’s Guide
Just type in the class name in the field and press ENTER. The carat will locate to the
inside of the class block. Now that you know how to create classes, you’ll need to know
how to add members, starting with methods.
Writing Methods
You can divide your algorithms into blocks of code called methods. In different programming
languages, methods are called functions, procedures, or subroutines. I’ll use the term method
as a generic term, except when I need to be more specific. You’ve already used methods
when coding Console.WriteLine, where WriteLine is a method of the Console class. A
method contains one or more statements. Reasons for creating methods include the ability to
modularize your code, isolate complex operations in one place, or group a common operation
that can be reused in multiple places. The following sections show you how to declare and
use methods.
Declaring and Using a Method
To start off, I’ll show you a very simple method so that you can see the syntax and
understand the program flow. Listing 3-5 will move the Console.Writeline statement from
the Main method discussed in Chapter 2 into a new containing method and then add a
statement to the Main method that calls the new method.
Listing 3-5 Declaring and calling a method

C# (Program.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
MessagePrinter msgPrint = new MessagePrinter();
msgPrint.PrintMessageInstance();
}
}

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×