c# datamatrix // Accessing the static class member in C#

Paint DataMatrix in C# // Accessing the static class member

// Accessing the static class member
Data Matrix 2d Barcode Creation In C#.NET
Using Barcode generator for .NET framework Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Reader In Visual C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Example of a Static Field
Creating 1D Barcode In Visual C#.NET
Using Barcode generation for .NET Control to generate, create Linear 1D Barcode image in .NET applications.
www.OnBarcode.com
Code 128 Code Set C Maker In C#
Using Barcode drawer for .NET framework Control to generate, create USS Code 128 image in .NET applications.
www.OnBarcode.com
The following code expands the preceding class D by adding two methods: One method sets the values of the two data members. The other method displays the values of the two data members. class D { int Mem1; static int Mem2; public void SetVars(int v1, int v2) // Set the values { Mem1 = v1; Mem2 = v2; } Access as if it were an instance field public void Display( string str ) { Console.WriteLine("{0}: Mem1= {1}, Mem2= {2}", str, Mem1, Mem2); } } Access as if it were an instance field class Program { static void Main() { D d1 = new D(), d2 = new D(); // Create two instances. d1.SetVars(2, 4); d1.Display("d1"); d2.SetVars(15, 17); d2.Display("d2"); d1.Display("d1"); } } // Set d1's values. // Set d2's values. // Display d1 again and notice that the // value of static member Mem2 has changed!
Paint PDF 417 In C#.NET
Using Barcode creation for VS .NET Control to generate, create PDF417 image in VS .NET applications.
www.OnBarcode.com
Make GS1 128 In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create EAN 128 image in VS .NET applications.
www.OnBarcode.com
CHAPTER 6 MORE ABOUT CLASSES
DataMatrix Creator In C#.NET
Using Barcode creation for .NET framework Control to generate, create ECC200 image in VS .NET applications.
www.OnBarcode.com
2/5 Standard Generation In C#
Using Barcode maker for Visual Studio .NET Control to generate, create 2 of 5 Standard image in Visual Studio .NET applications.
www.OnBarcode.com
The preceding code produces the following output: d1: Mem1= 2, Mem2= 4 d2: Mem1= 15, Mem2= 17 d1: Mem1= 2, Mem2= 17
Paint Data Matrix 2d Barcode In None
Using Barcode maker for Online Control to generate, create Data Matrix 2d barcode image in Online applications.
www.OnBarcode.com
Data Matrix 2d Barcode Encoder In Java
Using Barcode creator for BIRT Control to generate, create Data Matrix image in Eclipse BIRT applications.
www.OnBarcode.com
Lifetimes of Static Members
Printing Code 128 Code Set C In Objective-C
Using Barcode creation for iPad Control to generate, create Code 128A image in iPad applications.
www.OnBarcode.com
USS Code 39 Reader In VB.NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Instance members come into existence when the instance is created, and go out of existence when the instance is destroyed. Static members, however, exist and are accessible even if there are no instances of the class. Figure 6-4 illustrates a class D, with a static field, Mem2. Although Main does not define any instances of the class, it assigns the value 5 to the static field and prints it out.
Decode Barcode In VB.NET
Using Barcode Control SDK for .NET Control to generate, create, read, scan barcode image in VS .NET applications.
www.OnBarcode.com
Code 3 Of 9 Encoder In None
Using Barcode generation for Word Control to generate, create Code-39 image in Office Word applications.
www.OnBarcode.com
Figure 6-4. Static field with no class instances The code in Figure 6-4 produces the following output:
Scanning Data Matrix 2d Barcode In Visual C#
Using Barcode reader for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
GTIN - 128 Generation In Visual Basic .NET
Using Barcode generation for VS .NET Control to generate, create EAN128 image in VS .NET applications.
www.OnBarcode.com
Mem2 = 5
Code128 Decoder In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
PDF-417 2d Barcode Recognizer In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Note Static members exist even if there are no instances of the class.
Data Matrix Scanner In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Decoding UPCA In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
CHAPTER 6 MORE ABOUT CLASSES
Static Function Members
Besides static fields, there are also static function members. Static function members, like static fields, are independent of any class instance. Even if there are no instances of a class, you can call a static method. Static function members cannot access instance members. They can, however, access other static members. For example, the following class contains a static field and a static method. Notice that the body of the static method accesses the static field. class X { static public int A; // Static field static public void PrintValA() // Static method { Console.WriteLine("Value of A: {0}", A); } } Accessing the static field The following code uses class X, defined in the preceding code. Figure 6-5 illustrates the code. class Program { static void Main() { X.A = 10; X.PrintValA(); } } Class name
// Use dot-syntax notation // Use dot-syntax notation
This code produces the following output:
Value of A: 10
CHAPTER 6 MORE ABOUT CLASSES
Figure 6-5. Static methods of a class can be called even if there are no instances of the class.
Other Static Class Member Types
The types of class members that can be declared static are shown checked in Table 6-2. The other member types cannot be declared static. Table 6-2. Class Member Types That Can Be Declared Static
Data Members (Store Data)
Fields Constants
Function Members (Execute Code)
Methods Properties Constructors Finalizers Operators Indexers Events
CHAPTER 6 MORE ABOUT CLASSES
Constants
Of the nine class member types, two of them are data member types. The first, which has already been discussed, is fields. The second is constants. You can think of a constant as a type of variable for which the value cannot be changed. A constant must be initialized at its declaration. A constant cannot be changed after its declaration. There are two kinds of constants: Member constants, which are like fields except constant Local constants, which are like local variables except constant The core declaration for a constant is shown following. Note that the syntax is the same as that of a field or variable declaration, except for the following: The addition of the keyword const in front The mandatory initializer
Note The keyword const is not a modifier, but part of the core declaration. It must be placed immediately
before the type.
Keyword const Type Identifier = Value; Initializer required
Note Unlike C and C++, in C# there are no global constants. Every constant must be declared within
a type.
CHAPTER 6 MORE ABOUT CLASSES
Constant members, like fields, are declared in the class declaration, as follows: class MyClass { const int IntVal = 100; } Type Initializer const double PI = 3.1416;
Copyright © OnBarcode.com . All rights reserved.