Microsoft Visual C 2008 Chapter 4 Creating Objects

Microsoft® Visual C# 2008 Chapter 4: Creating Objects in C# 1

Overview Defining a Class Declaring Methods Using Constructors Using Static Class Members 2

Lesson: Defining a Class What Are Classes and Objects? What Are Value Types and Reference Types? How to Define a Class and Create an Object How to Organize Classes Using Namespaces How to Define Accessibility and Scope 3

What Are Classes and Objects? Classes: o Are like blueprints for objects o Contain methods and data Objects: o Are instances of a class o Created using the new keyword o Have actions 4

What Are Value Types and Reference Types? Value types Reference types o Contain a reference to o Directly contain the data o Stored on the stack o Stored on the heap o Must be initialized o Declared using new keyword o Cannot be null o. NET garbage collection o An int is a value int i; itype = 42; i handles destruction o A class is a reference Cost. Object c; 42 c type 42 5

How to Define a Class and Create an Object How to define a class public class Customer { public string name; public decimal credit. Limit; public uint customer. ID; } How to instantiate a class as an object Customer next. Customer = new Customer(); How to access class variables next. Customer. name = "Suzan Fine"; 6

How to Organize Classes Using Namespaces Declaring a namespace Company. Name { public class Customer () { } } Nested namespaces namespace Company. Name { namespace Sales { public class Customer () { } } } // Or namespace Company. Name. Sales {. . . } The using statement using System; using Company. Name. Sales; 7

How to Define Accessibility and Scope Access modifiers are used to define the accessibility level of class members Declaration Definition public Access not limited. private Access limited to the containing class. . internal Access limited to this program. protected Access limited to the containing class and to types derived from the containing class protected internal Access limited to the containing class, derived classes, or to members of this program 8

Practice: Defining Classes and Creating Objects Hands-on Practice n In this practice, you will define a class, instantiate an instance of the class, and assign values to the class members 9

Lesson: Declaring Methods How to Write a Method How to Pass Parameters to a Method How to Pass Parameters by Reference How to Pass a Reference Type How to Overload a Method 10

How to Write a Method A method is a command for action class Lion { private int weight; public bool Is. Normal. Weight () { if ((weight < 100)||(weight > 250)) { return false; } return true; } public void Eat() { /* some action */ } public int Get. Weight() { return this. weight; } }. . . ……… Lion big. Lion = new Lion(); bool weight. Normal = big. Lion. Is. Normal. Weight(); big. Lion. Eat(); int weight = big. Lion. Get. Weight(); 11

How to Pass Parameters to a Method Passing by value class Lion { private int weight; public void Set. Weight(int new. Weight) { weight = new. Weight; } }. . …. Lion big. Lion = new Lion(); int big. Lion. Weight = 250; big. Lion. Set. Weight( big. Lion. Weight ); 12

How to Pass Parameters by Reference Using the ref keyword public void Get. Address(ref int number, ref string street) { number = this. number; street = this. street; }. . . int s. Number = 0; string street. Name = null; zoo. Get. Address( ref s. Number, ref street. Name ); // s. Number and street. Name have new values Definite assignment Using the out parameter keyword o Allows you to initialize a variable in a method 13

How to Pass a Reference Type When you pass a reference type to a method, the method can alter the actual object class Zoo { public void Add. Lion( Lion new. Lion ) { new. Lion. location = "Exhibit 3"; . . . } }. . . Zoo my. Zoo = new Zoo(); Lion baby. Lion = new Lion(); my. Zoo. Add. Lion( baby. Lion ); // baby. Lion. location is "Exhibit 3” 14

How to Overload a Method Overloading enables you to create multiple methods within a class that have the same name but different signatures class Zoo { public void Add. Lion(Lion new. Lion) {. . . } public void Add. Lion(Lion new. Lion, int exhibit. Number) {. . . } } 15

Practice: Writing and Calling a Method Hands-on Practice n In this practice, you will add a method to animal class 16

Lesson: Using Constructors How to Initialize an Object How to Overload a Constructor 17

How to Initialize an Object Instance constructors are special methods that implement the actions required to initialize an object o Have the same name as the name of the class o Default constructor takes no parameters public class Lion { public Lion() { Console. Write. Line("Constructing Lion"); } } Readonly o Used to assign a value to a variable in the constructor 18

How to Overload a Constructor Create multiple constructors that have the same name but different signatures Specify publicoclass Lionan { initializer with this private string name; private int age; public Lion() : this( "unknown", 0 ) { Console. Write. Line("Default: {0}", name); } public Lion( string the. Name, int the. Age ) { name = the. Name; age = the. Age; Console. Write. Line("Specified: {0}", name); } } 19

Practice: Using Constructors Hands-on Practice n In this practice, you will modify the solution to the last practice, so that it uses constructors to initialize animal objects properly 20

Lesson: Using Static Class Members How to Use Static Class Members How to Initialize a Class 21

How to Use Static Class Members Static Members o Belong to the class o Initialize before an instance of the class is created o Shared by all instances of the class Lion { public static string family = "felidae"; }. . . // A Lion object is not created in this code Console. Write. Line( "Family: {0}", Lion. family ); 22

How to Initialize a Class Static Constructors o Will only ever be executed once o Run before the first object of that type is created o Have no parameters o Do not take an access modifier o May co-exist with a class constructor o Used to initialize a class 23

Practice: Using Static Class Members Hands-on Practice n In this practice, you will modify a class to use static class members 24

Review Defining a Class Declaring Methods Using Constructors Using Static Class Members 25

Lab 3. 1: Creating Classes in C# Hands-on Practice n Exercise 1: Creating the Bank Account Objects 26
- Slides: 26