មេរៀន VC#.NET-Properties ក្នុង Attribute

មេរៀន VC#.NET-Properties ក្នុង Attribute
-ប្រកាស Declare ទទួលទៅ control អនុញ្ញាតសរសេរក្នុង properties ។
-បង្កើត interface ដែលប្រកាស declare properties ។
-អនុវត្ត properties នៅក្នុង structs និងបណ្តា class ដែល
ទទួលពី interface ដែលមានផ្ទុក properties ។
-អនុវត្ត interface មួយដែលប្រើការអនុវត្តន៍ជាក់ស្តែង interface ។
1. ប្រៀបធៀប Field និង Method
ចូរមើល struct ដែលបង្ហាញខាងក្រោមនេះប្រើសំរាប់បង្ហាញនៅលើ ទីតាំងកូរអរដោនេ (X, Y) ។ បញ្ហាជាមួយ struct នេះគឺវាមិនធ្វើទៅតាម
លក្ខណ:តួនាទីនៃ encapsulation; វាមិនរក្សាទុកទិន្នន័យដោយឡែក data private:
struct ScreenPosition
{
public ScreenPosition(int x, int y)
{
this.X = rangeCheckedX(x);
this.Y = rangeCheckedY(y);
}
public int X;
public int Y;
private static int rangeCheckedX(int x)
{
if (x < 0 || x > 1280)
{
throw new ArgumentOutOfRangeException(“X”);
}
return x;
}
private static int rangeCheckedY(int y)
{
if (y < 0 || y > 1024)
{
throw new ArgumentOutOfRangeException(“Y”);
}
return y;
}
}
ទិន្នន័យរួម Public data គឺជាគំនិតមិនប្រសើរ ពីព្រោះវាមិនអាចប្រើ ត្រួតពិនិត្យ checked និង controlled ។ ឧទាហរណ៍ ScreenPosition constructor ឆែកមើលជួរលំដាប់ parameters របស់វា ។ ប៉ុន្តែគ្មានជួរលំដាប់ ណាដែលធ្វើនៅលើ “raw” អនុញ្ញាតទៅក្នុង public fields ។
ScreenPosition origin = new ScreenPosition(0, 0);

int xpos = origin.X;
origin.Y = -100; // Oops
មធ្យោបាយរួមដើម្បីដោះស្រាយបញ្ហានេះគឺបង្កើត fields private និង បន្ថែមវិធី method មួយរួចហើយផ្លាស់ប្តូរវិធី method ដើម្បីអាន read និងសរសេរតំលៃនីមួយៗនៃ private field ដែលបានបង្ហាញ ។ ផ្លាស់ប្តូរវិធី methods អាចឆែកមើលជួរលំដាប់ដែលតំលៃ field ពិតប្រាកដពីព្រោះ constructor ដែលឆែករួចជាស្រេចតំលៃ initial field ។ ឧទាហរណ៍ (GetX) និងផ្លាស់ប្តូរ (SetX) សំរាប់ X field ។ កត់ចំណាំថារបៀបឆែក SetX checks តំលៃ parameter របស់វា:
struct ScreenPosition
{

public int GetX()
{
return this.x;
}
public void SetX(int newX)
{
this.x = rangeCheckedX(newX);
}

private static int rangeCheckedX(int x) { … }
private static int rangeCheckedY(int y) { … }
private int x, y;
}
ឧទាហរណ៍ខាងក្រោមនេះបង្កើនតំលៃនៃ X ដោយ 10 ។ ដើម្បីធ្វើដូច្នេះ វាបានអាចតំលៃនៃ X ដោយប្រើ GetX method រួចសរសេរតំលៃនៃ X ដោយ ប្រើវិធីផ្លាស់ប្តូរ SetX modifier method:
int xpos = origin.GetX();
origin.SetX(xpos + 10);
ប្រៀបធៀបតំលៃនេះជាមួយតំលៃសមមូលនៃ code ប្រសិនបើ X field គឺជា public:
origin.X += 10;
2. អ្វីដែលហៅថា Properties?
property គឺជាការឆ្លងកាត់ logical field និងវិធី physical method អ្នកប្រើ property មួយកំណត់ជាក់ច្បាស់មធ្យោបាយដែលអ្នកប្រើ field មួយ។          បន្ទាប់ពី code segment បង្ហាញក្នុង ScreenPosition struct សរសេរឡើងវិញដោយប្រើ properties ។ កាលណាដែលអាននេះ code កំណត់ចំណាំថាដូចខាងក្រោមនេះ:
· x និង y គឺជា fields ដោយឡែក
· X និង Y គឺជា field រួមដូចជា properties ។ X និង Y គឺមិនមែនជា methods ។ វាគ្មានសញ្ញាវង់ក្រចក វាផ្ទុកគ្រាប់តែទទួលនិងបង្កើត ។
struct ScreenPosition
{
public ScreenPosition(int X, int Y)
{
this.x = rangeCheckedX(X);
this.y = rangeCheckedY(Y);
}
public int X
{
get { return this.x; }
set { this.x = rangeCheckedX(value); }
}
public int Y
{
get { return this.y; }
set { this.y = rangeCheckedY(value); }
}
private static int rangeCheckedX(int x) { … }
private static int rangeCheckedY(int y) { … }
private int x, y;
}
ទទួល get Accessor
ប្រសិនបើប្រើ property មួយក្នុងការអាន read context ពេល compiler automatically ទៅក្នុង field របស់អ្នកដូចជា code នៅក្នុងហៅថា
ទទួល get accessor នៃ property នោះ ។ ឧទាហរណ៍ compiler automatically ដូចនេះ:
ScreenPosition origin = new ScreenPosition(0, 0);
int xpos = origin.X;
int ypos = origin.Y;
ធ្វើជាក់ស្តែងដូចនេះ:
int x = topLeft.x;
បង្កើត set Accessor
ប្រសិនបើអ្នកប្រើ property មួយនៅក្នុងការសរសេរ context ពេល
ដែល compiler automatically ក្នុង field code របស់អ្នកទៅក្នុងហៅទៅ set accessor នៃ property នោះ ។ ឧទាហរណ៍ compiler automatically ដូចនេះ:
origin.X = 40;
origin.Y = 100;
Read-Only Properties
អ្នកត្រូវបានអនុញ្ញាតដើម្បីប្រកាស declare property មួយដែលផ្ទុក ទទួល get accessor ។ ក្នុងករណីនេះអ្នកអាចប្រើ property គ្រាន់តែសំរាប់ អាចក្នុង context ។ ឧទាហរណ៍ ទីនេះមាន X property មួយនៃ ScreenPosition struct ដែលបានប្រកាស read-only property:
struct ScreenPosition
{

public int X
{
get { return this.x; }
}
}
X property មិនមានផ្ទុក set accessor; ព្យាយាមដើម្បីប្រើ X ក្នុងការសរសេរ context និងបរាជ័យ fail ។ ឧទាហរណ៍ :
origin.X = 140; // compile-time error
Write-Only Properties
អ្នកត្រូវបានអនុញ្ញាតដើម្បីប្រកាស declare property មួយដែលផ្ទុក set accessor តែមួយគត់ ។ ក្នុងករណីនេះអ្នកអាចគ្រាន់តែប្រើ property ក្នុងការសរសេរ context ។ ឧទាហរណ៍ X property នៃ ScreenPosition struct ដែលបានប្រកាសដូចជា write-only property:
struct ScreenPosition
{

public int X
{
set { this.x = rangeCheckedX(value); }
}
}
X property មិនផ្ទុក get accessor; ព្យាយាមប្រើ X ក្នុងការអាននៅ
ពេលបរាជ័យ fail ។ ឧទាហរណ៍:
Console.WriteLine(origin.X); // compile-time error
origin.X = 200; // compiles ok
origin.X += 10; // compile-time error
ស្វែងយល់ពី Property Restriction
¡ អ្នកអាចចាប់ផ្តើម property មួយនៃ struct  ឆ្លងកាត់ accessor ឧទាហរណ៍:
ScreenPosition location;
location.X = 40; // compile-time error, location not assigned

¡ អ្នកអាចប្រើ property មួយដូចជា ref ឬ out argument ឧទាហរណ៍:
MyMethod(ref location.X); // compile-time error
¡ អ្នកមិនអាចប្រកាស declare ដែលមាន properties ច្រើននៅក្នុង ការប្រកាស statement ទោល ។ ឧទាហរណ៍:
const int X { get { … } set { … } } // compile-time error
¡ អ្នកមិនអាចប្រកាស declare const ឬ read only properties ឧទាហរណ៍:
const int X {get{…} set{…}}//compile-time error
Properties គឺមិនមែន true fields ប៉ុន្តែវាមិនមែនជាវិធី true methods ។
¡ អ្នកអាចប្រកាស declare getset accessor ដែលទាំងពីរនេះ មាននៅក្នុង property ។
public int X
{
Console.WriteLine(“common code”);//compile-time error
get { …}
set {…}
}
¡ អ្នកមិនអាចប្រកាស declare ក្នុង property មួយដែលគឺជាប្រភេទ ទំនេរ ។ ឧទាហរណ៍:
public void X{get{…} set {…}}//compile-time error
¡ មិនអាចប្រកាស declare ក្នុង property មួយដោយគ្មាន parameter ឧទាហរណ៍:
public /*nothing*/ X {get{…}set{…}}
//compile-time error
¡ អ្នកមិនអាចប្រកាស declare ក្នុង property មួយជាពីរឬច្រើន parameters:
public int,string X { get{…}set{…}}
//compile-time error
3. ប្រើ Static Properties អ្នកអាចប្រកាស declare ពីប្រភេទនៃ fields ក្នុង class មួយឬ struct: instance fields (មួយក្នុង object) និង static fields (មានមួយក្នុង ប្រភេទ) ។ អ្នកអាចប្រើ properties ទៅក្នុង mimic instance fields ហើយ អ្នកអាចប្រើ static properties ទៅក្នុង mimic static fields ។ ដែល syntax សំរាប់ static property គឺច្បាស់លាស់ដូចជាសំរាប់ instance property លើកលែងតែអ្នកប្រកាស property នៅខាងមុខជាមួយ ពាក្យគន្លឹះ static ។ ឧទាហរណ៍ ទីនេះមាន ScreenPosition struct ដែលពេលនេះ ជាមួយ static read-only property ដែលតំណាងទំហំនៃ screen:
struct ScreenPosition
{
public static ScreenPosition Limits
{
get { return limits;}
}
private static int rangeCheckedX(int x)
{
if (x < 0 ││ x > limits.x)
{
throw new ArgumentOutOfRangeException(“X”);
}
return x;
}
private static int rangeCheckedY(int y)
{
if (y < 0 ││ y > limits.y)
{
throw new ArgumentOutOfRangeException(“Y”);
}
return y;
}
private static ScreenPosition vgaLimits =
new ScreenPosition(600, 800);
private static ScreenPosition limits = vgaLimits;
private int x, y;
}
4. ការប្រកាស Interface Properties អ្នកអាចប្រកាស declare properties នៅក្នុង interface ។ ដើម្បីធ្វើ ដូចនេះ អ្នកប្រកាស declare ទទួលពាក្សគន្លឹះ getset  ដែលមទាំងពីរនេះ ប៉ុន្តែការប្តូររូបរាងនៃ getset accessor ជាមួយសញ្ញា semicolon ។ ឧទាហរណ៍:
interface IScreenPosition
int X { get; set;}
int Y { get; set;}
}
class ឬ Struct ដែលអនុវត្ត interface នេះត្រូវការអនុវត្ត accessors ។ ឧទាហរណ៍:
struct ScreenPosition :IScreenPosition
{
public int X
{
get { return x; }
set { x = rangeCheckedX(value);}
}
public int Y
{
get {return y;}
set {y= rangeCheckedY(value);}
}
private int x, y;
}
ប្រសិនបើអនុវត្ត interface properties ក្នុង class មួយអ្នកអាច
ប្រកាស declare property ដូចជាការអនុវត្តន៍ជាក់ស្តែង ដែលអនុញ្ញាតអោយ classes មានលើការអនុវត្តន៍ ឧទាហរណ៍:
class ScreenPosition : IScreenPosition
{
public virtual int X
{
get{…}
set {…}
}
public virtual int Y
{
get {…}
set {…}
}
private int x, y;
}
អ្នកអាចជ្រើសរើសដើម្បីអនុវត្តក្នុង property ដោយប្រើ interface ជាក់ស្តែងក្នុងការអនុវត្តន៍ syntax ក្នុងជំពូកទី 12 ។ ការអនុវត្តន៍ជាក់ស្តែង property មួយគឺ non-public និង non-virtual ឧទាហរណ៍:
struct ScreenPosition :IScreenPosition
{
int IScreenPosition.X
{
get { return x; }
set { x = rangeCheckedX(value); }
}
int IScreenPosition.Y
{
get { return y; }
set { y = rangeCheckedY(value);}
}
private int x, y;
}
ក្នុងលំហាត់បន្ទាប់ អ្នកនឹងប្រើនិយមន័យ properties ឡើងវិញនៃ ប្រអប់ Microsoft Windows text និង Windows forms ដើម្បីបង្កើត គំរូកម្ម វិធីប្រើប្រាស់ដែលមើលឃើញទំហំ size នៃសន្លឹកចម្បង ។
ប្រើ properties
1. ចាប់ផ្តើមក្នុង Start Microsoft Visual Studio .NET។
2. បើកក្នុង Properties project ដែលមានទីតាំងក្នុង \Microsoft Press\Visual C# Step by Step\Chapter14\Properties folder ក្នុង My Documents folder របស់អ្នក ។
3. នៅលើ Debug menu ចុចលើ Start Without Debugging ។ project builds និងរត់ដំណើរការ ។ សន្លឹក Windows form មើលឃើញប្រអប់ទ text ទទេពីរដែលមានឈ្មោះ labeled Width និង Height ។
4. បង្កើតទំហំ form ឡើងវិញ ។ ក្នុងប្រអប់ text ពីរនៅទទេ ។
5. បិទ form អ្នកត្រឡប់ទៅកម្មវិធី Visual Studio .NET ។
6. បើកប្រភព file Form1.cs នៅក្នុង Code pane ដែលមើលនៃការ ប្រកាសនៃ Width និងប្រះប់ Height text ។
6. ការប្រកាសនេះគឺនៅជិតខាងលើនៃ Form1.cs ដែលមើលឃើញដូច នេះ:
private System.Windows.Forms.TextBox width;
private System.Windows.Forms.TextBox height;
ដែល TextBox class ផ្ទុក public read/write string ដែលហៅថា Text (វាទទួល property នេះពីមូលដ្ឋាន base class: TextBoxBase):
abstract class TextBoxBase …
{
public override string Text
{
get {…}
set{…}
}
}
class TextBox : TextBoxBase
{
}
7. បន្ថែមពីរ statements ទៅក្នុងទំហំដែល resize method ឡើងវិញ statement ទីមួយផ្តល់ string “232″ ទៅ Text property នៃប្រអប់ Width text ។ ទីពីរ statement ផ្តល់ string “96″ ទៅ Text property នៃប្រអប់ Height text ។ ប្រើវិធី resize method នឹងមើលឃើញច្បាស់ដូចនេះ:
private void resize()
{
width.Text = “232″;
height.Text = “96″;
}
8. នៅលើ Debug menu ចុចលើ Start Without Debugging ។  project builds និងរត់ដំណើរការ ។ សន្លឹក Windows form មើលឃើញប្រអប់ text ពីរដែលផ្ទុកតំលៃ 232 និង 96 ។ បង្កើត set accessor នៃប្រអប់ TextBox ។ ដែល Text property កំណត់ច្បាស់ លាស់មានច្រើនជាង string មួយ ។ វាក៏បានធ្វើអោយច្បាស់ string របស់វាដែលមើលឃើញនិងក្លាយជា visible ។
ជំហានបន្ទាប់សរសេរ strings ត្រឹមត្រូវទៅក្នុង TextBox ។ Text   properties ក្នុងគោលបំណងបង្ហាញសន្លឹក window តែងតែមើលឃើញតំលៃ ត្រឹមត្រូវ ។
9. បិទ form អ្នកត្រឡប់ទៅក្នុងកម្មវិធី Visual Studio .NET ។
10. ប្រើ Scroll ឡើងលើ Form1.cs source file មើលការប្រកាស នៃ Form1 class ។ Form1 class ពី System.Windows.Forms.Form class:
public class Form1: System.Windows.Forms.Form
{
}
ក្នុង System.Windows.Forms.Form class ដែលផ្ទុក public read/write Size property:
private void resize()
{
width.Text = “232″;
height.Text = “96″;
}
ទំហំ property ត្រឡប់ទៅ instance នៃ struct មួយដែលហៅថា ទំហំដែលស្ថិតក្នុង System.Drawing namespace ។ ទំហំ Size struct ផ្ទុក public read/write int មួយដែលហៅថា Height និងមួយទៀតហៅថា Width:
struct Size
{
public int Height
{
get { … }
set { … }
}
public int Width
{
get { … }
set { … }
}
}
}
11. Scroll មកក្រោមប្រភព file Form1.cs មើល resize method ទីពីរ ។
12. ផ្លាស់ប្តូរពីរ statements ដើម្បី Size.Width ត្រូវបានផ្តល់ទៅ
អោយ width.Text និង Size.Height ត្រូវបានផ្តល់ height.Text property ។ អ្នកនឹងត្រូវការដើម្បីប្តូរពី int ទៅ string មួយ ។ មធ្យោបាយងាយបំផុតដើម្បីធ្វើដូចនេះប្រើ ToString method ។
private void resize()
{
int w = this.Size.Width;
width.Text = w.ToString();
int h = this.Size.Height;
height.Text = h.ToString();
}
13. នៅលើ Debug menu ចុចលើ Start Without Debugging ។ project builds និងរត់ដំណើរការ ។
14. Resize ទំហំឡើងវិញ Windows form ។ ដូចអ្នក resize ឡើង វិញប្រអប់ text boxes ប្តូរដើម្បីបង្ហាញការផ្លាស់ប្តូរទំហំ។
15. បិទ form ។ អ្នកត្រឡប់ទៅកម្មវិធី Visual Studio .NET ។ ចុងក្រោយអ្នកនឹងមើលឃើញទំហំនៃ form ដូចជាចំណង់ជើង form ។
16. មើល resize method ទីពីរក្នុងប្រភព file Form1.cs ។
17. បន្ថែមពីរឬច្រើន statements ទៅ resize method ។ statement ទីមួយនឹងប្រើ string.Forma ដើម្បីបង្កើត string ទោល ដែលផ្ទុក width និង height នៃ form ។ statement ទីពីរនឹងសរសេរ string នេះទៅ public read/write string Text នៃ form ។
private void resize()
{
int w = this.Size.Width;
width.Text = w.ToString();
int h = this.Size.Height;
height.Text = h.ToString();
string s = string.Format(“({0}, {1})”, w, h);
this.Text = s;
}
18. នៅលើ Debug menu ចុចលើ Start Without Debugging ។  project builds និងរត់ដំណើរការ ។
a
19. បិទ form អ្នកត្រឡប់ទៅកម្មវិធី Visual Studio .NET ។
សង្ខេបជំពូកទី ១៣
ប្រធានបទ
ការអនុវត្តន៍
ប្រកាស Declare read/write property

ប្រកាស Declare ប្រភេទនៃ property ឈ្មោះរបស់វា getAccessor និង set accessor ឧទាហរណ៍: struct ScreenPosition
{

public int X
{
get { … }
set { … }
}

}
ប្រកាស read-only property

ប្រកាស Declare property ជាមួយ get accessor  ឧទាហរណ៍:struct ScreenPosition {

public int X
{
get { … }
}

}
}
ប្រកាស Declare write-only property

ប្រកាស Declare property ជាមួយ set accessor ឧទាហរណ៍:struct ScreenPosition {

public int X
{
set { … }
}

}
ប្រកាស Declare property ក្នុង interface

ប្រកាស Declare property ជាមួយ ពាក្យគន្លឹះ get ឬ set ឧទាហរណ៍:interface IScreenPosition {
int X { get; set; } // no body
int Y { get; set; } // no body
}
អនុវត្ត interface property

ក្នុង class ឬ struct ដែលអនុវត្ត interface ប្រកាស declare property និងអនុវត្ត accessors។ឧទាហរណ៍: struct ScreenPosition : IScreenPosition
{
public int X
{
get { … }
set { … }
}
public int Y
{
get { … }
set { … }
}
}

Related product you might see:

Share this product :

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. ទឹកអំពៅ - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger