Lesson 3: Using Classes and Structures

Lesson 3: Using Classes and Structures

You have seen how the .NET Framework base class library provides a plethora of standard types to help you in the development of your applications. You can also create user-defined types that implement custom behaviors. Classes and structures represent the two principal user-defined types.

After this lesson, you will be able to

  • Create a new class or structure

  • Create an instance of a class or a structure

  • Explain the difference between a class and a structure

  • Create a nested type

Estimated lesson time: 30 minutes

Classes are templates for objects. They describe the kind and amount of data that an object will contain, but they do not represent any particular instance of an object. A real-world example of a class might be Car the abstract idea of what a car is. You know that a car has an engine, four wheels, a body color, an individual fuel efficiency, and a dozen other properties. Although the Car class would describe all these properties, as well as have descriptions of actions that the car might perform (roll forward, turn on windshield wipers, and so on), the class would not represent any particular car. Your car, on the other hand, is an object. It has a specific color, a specific fuel efficiency, a specific engine, and four specific wheels. A different car might have different values for each of these properties, but both would be recognizable as being an instance of the Car class.

Members

Classes describe the properties and behaviors of the objects they represent through members. Members are methods, fields, properties, and events that belong to a particular class. Fields and properties represent the data about an object the color of the car, its fuel efficiency, and whether it has an automatic or manual transmission, for example. A method represents something the object can do, such as move forward or turn on headlights. An event represents something interesting that happens to the object, such as overheating or crashing.

NOTE
This chapter discusses fields and methods. Properties and events are covered in Chapter 3.

Creating Classes

You create a new class by using the Class (Visual Basic .NET) or class (C#) keyword. For example:

Visual Basic .NET

Public Class Widget ' Class member implementation goes here End Class

Visual C#

public class Widget { // Class member implementation goes here }

In this example, you use the Class (class) keyword to create a user-defined class. Widget is the name of the class, and the Public (public) keyword specifies the access level. Access levels are examined in greater detail in Lesson 5 of this chapter.

Creating Structures

Creating structures is very similar to creating classes. You use the Structure (Visual Basic .NET) or struct (C#) keyword. For example:

Visual Basic .NET

Public Structure Vector ' Structure implementation goes here End Structure

Visual C#

public struct Vector { // Structure implementation goes here }

Adding Members

In Visual Basic .NET, a class comprises everything between the Class keyword and the End Class keyword. In C#, a class comprises everything within braces ({}). Structures are similar. Within the bounds of a class or a structure, you add the members. The following example demonstrates adding a member field to your Widget class:

Visual Basic .NET

Public Class Widget Public Spin As Integer End Class

Visual C#

public class Widget { public int Spin; }

Your Widget class now contains a member variable named Spin. This variable has a Public (public) access level and can contain an Integer (int) value. Adding methods as members of your class or structure is discussed in Lesson 4 of this chapter.

Nested Types

Types can contain other types. Types within types are called nested types. Using classes as an example, a nested class usually represents an object that the parent class might need to create and manipulate, but which an external object would never need to create independently. An abstract example might be a Wheel class. A Wheel class might need to create and maintain a collection of Spoke objects internally, but outside users would probably never need to create a Spoke object independent of a wheel. A more realistic example might be an AccountManager class that controls all the interaction with Account objects. You might not want to allow Account objects to be created independently of the AccountManager class, so you would make Account a nested class inside AccountManager. This does not mean that outside objects can never instantiate objects based on nested classes this depends on the access level of both the parent class and the nested class. See Lesson 5 of this chapter for more detail. An example of a nested class follows:

Visual Basic .NET

Public Class Widget ' Widget Class code goes here Private Class Widgurt ' Widgurt class code goes here End Class End Class

Visual C#

public class Widget { // Widget class code goes here private class Widgurt { // Widgurt class code goes here. } }

Instantiating User-Defined Types

You declare and instantiate a user-defined type the same way that you declare and instantiate a .NET Framework type. For both value types (structures) and reference types (classes), you need to declare the variable as a variable of that type and then create an instance of it with the New (new) keyword. Examples are as follows:

Visual Basic .NET

Public Class Demo Public Structure ValueDemo Public X As Integer End Structure Public Class RefDemo Public Y As Integer End Class Public Sub InstantiateTypes() ' This line declares a ValueDemo variable Dim DemoStructure As ValueDemo ' This line creates an instance of ValueDemo on the stack DemoStructure = New ValueDemo() ' The variable is ready to receive data. DemoStructure.X = 15 ' This line declares a RefDemo variable, but doesn't ' create an instance of the class Dim DemoClass As RefDemo ' This line actually creates the object DemoClass = New RefDemo() ' And you can now assign value to its members DemoClass.Y = 15 End Sub End Class

Visual C#

public class Demo { public struct ValueDemo { public int X; } public class RefDemo { public int Y; } public void InstantiateTypes() { // This line declares a ValueDemo variable ValueDemo DemoStructure; // This line creates an instance of ValueDemo on the stack DemoStructure = new ValueDemo(); // The variable is ready to receive data DemoStructure.X = 15; // This line declares a RefDemo variable, but doesn't create // an instance of the class RefDemo DemoClass; DemoClass = new RefDemo(); // And you can now assign value to its members DemoClass.Y = 15; } }

Classes vs. Structures

On the surface, classes and structures appear to be very similar. Both can contain members such as fields and methods, both require a constructor to create a new instance of themselves, and like all types in the .NET Framework, both inherit from Object. The key difference between classes and structures is that classes are reference types and structures are value types. On a low level, this means that the instance data for classes is allocated on the heap, whereas the instance data for structures is allocated on the stack. Access to the stack is designed to be light and fast, but storage of large amounts of data on the stack can impede overall application performance.

In practical terms, that structures are best used for smaller, lightweight objects that contain relatively little instance data or for objects that do not persist for long. Classes are best used for larger objects that contain more instance data and are expected to exist in memory for extended periods.

Lesson Summary

  • User-defined types include classes and structures. Both can have members, which are fields, properties, methods, or events. Classes are reference types, and structures are value types.

  • The Class keyword is used to create new classes in Visual Basic .NET and the class keyword is used for Visual C#. Structures are created by using the Structure keyword in Visual Basic .NET and the struct keyword in Visual C#. Both classes and structures can contain nested types.

  • User-defined types are instantiated in the same manner as predefined types, except that both value types and reference types must use the New (new) keyword upon instantiation.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net