57: Accessing Fields from within the Containing Class in Visual C#.NET

Make PDF-417 2d barcode in Visual C#.NET 57: Accessing Fields from within the Containing Class
Listing 57: Accessing Fields from within the Containing Class
Draw PDF-417 2d Barcode In Visual C#.NET
Using Barcode maker for .NET framework Control to generate, create PDF 417 image in Visual Studio .NET applications.
class Employee { public string FirstName; public string LastName; public string Salary; public string GetName() { return FirstName + " " + LastName; } }
Bar Code Printer In C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create bar code image in VS .NET applications.
There is nothing particularly special about this method compared to what you learned in 4, except that now the GetName() method accesses fields on the object instead of just local variables In addition, the method declaration is not marked with static As you will see later in this chapter, static methods cannot directly access instance fields within a class
Make PDF 417 In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
5: Classes
Making PDF417 In .NET
Using Barcode encoder for .NET Control to generate, create PDF 417 image in VS .NET applications.
Instead, it is necessary to obtain an instance of the class in order to call any instance member, whether a method or a field Given the addition of the GetName() method, you can update ProgramMain() to use the new method, as shown in Listing 58 and Output 52
PDF 417 Maker In VB.NET
Using Barcode encoder for .NET framework Control to generate, create PDF-417 2d barcode image in .NET applications.
Listing 58: Accessing Fields from Outside the Containing Class
Paint Barcode In Visual C#
Using Barcode maker for .NET Control to generate, create bar code image in VS .NET applications.
class Program { static void Main() { Employee employee1 = new Employee(); Employee employee2; employee2 = new Employee(); employee1FirstName = "Inigo"; employee1LastName = "Montoya"; employee1Salary = "Too Little"; IncreaseSalary(employee1); ConsoleWriteLine( "{0}: {1}", employee1GetName(), employee1Salary); // } // }
Draw Bar Code In C#.NET
Using Barcode encoder for VS .NET Control to generate, create barcode image in .NET framework applications.
OUTPUT 52:
Code-39 Creation In Visual C#
Using Barcode printer for .NET framework Control to generate, create ANSI/AIM Code 39 image in .NET framework applications.
Inigo Montoya: Enough to survive on
Making Code 128 Code Set A In Visual C#
Using Barcode printer for .NET framework Control to generate, create Code 128C image in .NET framework applications.
Using the this Keyword
Painting Data Matrix ECC200 In C#
Using Barcode maker for .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
You can obtain the reference to a class from within instance members that belong to the class To indicate explicitly that the field or method accessed is an instance member of the containing class in C#, you use the keyword this this is simply an implicit field within every class definition that returns an instance of the object itself For example, consider the SetName() method shown in Listing 59
Decode GS1 - 12 In Visual Studio .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
Listing 59: Using this to Identify the Field s Owner Explicitly
Create Bar Code In Java
Using Barcode creation for Java Control to generate, create barcode image in Java applications.
class Employee { public string FirstName;
Bar Code Creator In Java
Using Barcode generation for Java Control to generate, create barcode image in Java applications.
Using the this Keyword
European Article Number 13 Scanner In Visual Studio .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
public string LastName; public string Salary; public string GetName() { return FirstName + " " + LastName; } public void SetName(string newFirstName, string newLastName) { thisFirstName = newFirstName; thisLastName = newLastName; } }
Encode Bar Code In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create bar code image in ASP.NET applications.
This example uses the keyword this to indicate that the fields FirstName and LastName are instance members of the class
Code 3 Of 9 Generation In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Code39 image in ASP.NET applications.
BEGINNER TOPIC Relying on Coding Style to Avoid Ambiguity In the SetName() method, you did not have to use the this keyword because FirstName is obviously different from newFirstName Consider, however, if instead of calling the parameter newFirstName you called it FirstName (using Pascal case), as shown in Listing 510
Read Data Matrix ECC200 In .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET framework applications.
Listing 510: Using this to Avoid Ambiguity
Code 128A Decoder In Visual Studio .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications.
class Employee { public string FirstName; public string LastName; public string Salary; public string GetName() { return FirstName + " " + LastName; } // Caution: Parameter names use Pascal case public void SetName(string FirstName, string LastName) { thisFirstName = FirstName; thisLastName = LastName; } }
Make DataMatrix In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.
5: Classes
Draw USS Code 39 In VS .NET
Using Barcode generator for VS .NET Control to generate, create Code-39 image in VS .NET applications.
In this example, it is not possible to refer to the FirstName field without explicitly indicating that the Employee object owns the variable this acts just like the employee1 variable prefix used in the ProgramMain() method (see Listing 58); it identifies the reference as the one on which SetName() was called Listing 510 does not follow the C# naming convention in which parameters are declared like local variables, using camel case This can lead to subtle bugs because assigning FirstName (intending to refer to the field) to FirstName (the parameter) will still compile and even run To avoid this problem it is a good practice to have a different naming convention for parameters and local variables than the naming convention for fields I demonstrate one such convention later in this chapter
Language Contrast: Visual Basic Accessing a Class Instance with Me
The C# keyword this is identical to the Visual Basic keyword Me
In Listing 59 and Listing 510, the this keyword is not used in the GetName() method it is optional However, if local variables or parameters exist with the same name as the field (see SetName() method of Listing 510), leaving off this would result in accessing the local variable/parameter rather than the field, so this would be required You also can use the keyword this to access a class s methods explicitly thisGetName() is allowed within the SetName() method, for example, allowing you to print out the newly assigned name (see Listing 511 and Output 53)