Shared (သို့) class များအကြား ဘုံအဖြစ်မျှဝေသုံးစွဲခြင်း
Shared အသုံးပြုပုံက ရိုးရှင်းပါတယ်။ သာမန်ရိုးရိုး class တွေကြားမှာတော့ shared ကိုမသုံးသလောက်ပါပဲ။ Enterprise Application တွေဖန်တီးတဲ့ အချိန်ကြရင်တော့ shared က တော်တော်လေးကို အသုံးဝင်ပါတယ်။ class တွေထဲမှာရေးထားတဲ့ member data, property, function, sub တွေကို ခေါ်သုံးချင်ရင် အဲဒီ class ကို object (instance) အဖြစ် ကြေငြာပြီးမှ သုံးလို့ရပါတယ်။ shared ကတော့ class name နဲ့ တိုက်ရိုက်ယူသုံးရပါတယ်။.Net Framework library ထဲမှာလည်း shared အနေနဲ့ သုံးထားတာ အများအပြားရှိပါတယ်။ String.Format လို function မျိုးတွေပါ။ အောက်က ဥပမာလေးကို ကြည့်လိုက်ရအောင်။
Public Class Person
Private Shared mCount As Integer
Sub New()
mCount += 1
End Sub
Public Shared ReadOnly Property Count() As Integer
Get
Return mCount
End Get
End Property
End Class
အပေါ်က person ဆိုတဲ့ class ထဲမှာ member data တစ်ခုပဲ ပါပါတယ်။ Integer အမျိုးအစား ၊ Private scope ပါ။ အဲဒီမှာ Shared ဆိုတဲ့ စာလုံးပါလာတာ သတိထားမိမှာပါ။ mCount ကို object(instance) တွေအနေနဲ့ယူသုံးမှာမဟုတ်ပဲ Person class တွေကြားမှ ဘုံမျှသုံးမယ်ဆိုတဲ့ အဓိပ္ပာယ်ပါ။ constructor တစ်ခုပါပါတယ်။ အဲဒီ constructor အလုပ်လုပ်တိုင်း mCount ကို ၁ ပေါင်းသွားပါမယ်။ Count ဆိုတဲ့ property တစ်ခု ပါပါသေးတယ်။ public scope, readonly အမျိုးအစားပါ။ အဲဒီမှာလည်း shared ဆိုတဲ့စာလုံး ပါပါတယ်။
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim per As New Person
Dim per2 As New Person
Dim per3 As New Person
MessageBox.Show(Person.Count)
End Sub
အပေါ်က code ကတော့ Form Load Event မှာရေးရမှာပါ။
Dim per As New Person
အဲဒီတစ်ကြောင်း run လို့ပြီးသွားတာနဲ့ person class ရဲ ့ constructor က အလုပ်လုပ်ပါပြီ။ constructor ထဲမှာ mCount ကို တစ်ပေါင်း သွားတဲ့အတွက် အခု mCount ထဲမှာ ၁ ရှိနေပါပြီ။
Dim per2 As New Person
ဒုတိယတစ်ကြောင်း run လို့ပြီးသွားတာနဲ့ အပေါ်ကအတိုင်းပဲ Person class ရဲ ့ constructor အလုပ်လုပ်ပါတယ်။ constructor ထဲမှာ mCount တန်ဖိုးကို တစ်ပေါင်းမှာဖြစ်ပြီး ၊ အပေါ်မှာတုန်းက mCount တန်ဖိုး ၁ ရှိခဲ့တဲ့အတွက် အခုတစ်ကြိမ်မှာ mCount တန်ဖိုး ၂ ဖြစ်သွားပါပြီ။ တကယ်လို့ mCount ကို class ထဲမှာ ကြေငြာခဲ့တုန်းက shared ဆိုတဲ့ စာလုံးမပါခဲ့ရင် person class တူပေမယ့် မတူညီတဲ့ object(instance) တွေကြားမှာ mCount တန်ဖိုးဟာ အမြဲတမ်း ၁ ပဲ ဖြစ်နေမှာပါ။ အဲဒါကြောင့် shared ရဲ့ သဘောသဘာဝကိုက object(instance) အဖြစ်မကြည့်ပဲ class တွေကြားမှာ shared (မျှဝေ) သုံးစွဲကြတာပါ။
Dim per3 As New Person
အပေါ်က တစ်ကြောင်း run ပြီးသွားရင်တော့ mCount တန်ဖိုးက ၃ ဖြစ်သွားပါပြီ။
MessageBox.Show(Person.Count)
အပေါ်က တစ်ကြောင်းကတော့ အဖြေကို message box နဲ့ ထုတ်ကြည့်တာပါ။ အဲဒီမှာ Person.Count ဆိုတာကို တွေ့ကြမှာပါ။ Count ဆိုတာ Person class ထဲမှာ ရေးထားတဲ့ public အမျိုးအစား readonly property ပါ။ ပုံမှန်အားဖြင့်တော့ class က ပိုင်တဲ့(class ထဲမှာရေးထားတဲ့) property တွေကို ယူသုံးချင်ရင် object(instance) ကနေတဆင့်ခေါ်သုံးရမှာပါ။ ဥပမာ (per.Count or per2.Count or per3.Count) အဲဒီလိုပဲဖြစ်သင့်ပါတယ်။ ဒါပေမယ့် Count property ကြေငြာတုန်းက shared ဆိုတဲ့စာလုံးထည့်ထားတဲ့အတွက် object(instance) ကနေတဆင့်ခေါ်သုံးရင် error တက်မှာပါ။ Class name ကနေတဆင့် တိုက်ရိုက်ယူသုံးမှပဲ ရမှာပါ။ အဲဒါကြောင့် class name ဖြစ်တဲ့ Person.Count ဆိုပြီးတော့ ခေါ်သုံးရခြင်း ဖြစ်ပါတယ်။
နောက်ထပ် ဥပမာတွေနဲ့ ထပ်ပြောပြပါအုံးမယ်။
Public Class Person
Private Shared mCount As Integer
Private mBirthdate As Date
Sub New()
mCount += 1
End Sub
Public Shared ReadOnly Property Count() As Integer
Get
Return mCount
End Get
End Property
Public Property BirthDate() As Date
Get
Return mBirthdate
End Get
Set(ByVal value As Date)
mBirthdate = value
End Set
End Property
Public ReadOnly Property Age() As Integer
Get
Return CInt(DateDiff(DateInterval.Year, mBirthdate, Now))
End Get
End Property
Public Shared Function CompareAge(ByVal per1 As Person, ByVal per2 As Person) As Boolean
Return per1.Age > per2.Age
End Function
End Class
Person class ကို အပေါ်က အတိုင်း ပြင်ကြည့်လိုက်ရအောင်။ ဘာတွေ ပိုလာသလဲဆိုတော့ mBirthDate ဆိုတဲ့ member data ၊ BirthDate နဲ့ Age ဆိုတဲ့ property နဲ့ CompareAge ဆိုတဲ့ shared function တစ်ခုပါ။ Age ဆိုတဲ့ property က လက်ရှိရောက်နေတဲ့ အချိန်ကနေ mBirthDate ထဲမှာ ရောက်နေတဲ့ အချိန် ခြားနားခြင်း(နှုတ်လဒ်) ကို return ပြန်ပေးမှာပါ။ နည်းနည်းပြောစရာရှိတာက CompareAge ဆိုတဲ့ function ပါ။ သူက ရိုးရိုး function မဟုတ်ပါဘူး။ shared function ပါ။ အဲဒီအတွက် CompareAge ကို ခေါ်သုံးတဲ့အချိန်မှာ class နာမည် နဲ့ တိုက်ရိုက် ခေါ်သုံးရမှာပါ (Person.CompareAge(..,…))
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim per1 As New Person
Dim per2 As New Person
per1.BirthDate = #5/1/1990#
per2.BirthDate = #5/1/1995#
If Person.CompareAge(per1, per2) Then
MessageBox.Show("Person1 is older than person 2")
Else
MessageBox.Show("Person 2 is older than person 1")
End If
End Sub
Form Load Event မှာလည်း အပေါ်ကလို ပြင်ရေးကြည့်လိုက်ရအောင်။ per1,per2 အဖြစ် person class ကို object တွေ ကြေငြာပါတယ်။ object 2 ခုစလုံးမှာ BirthDate တွေကို Assign လုပ်ပါတယ်။ ပြီးတော့ shared function ဖြစ်တဲ့ CompareAge ကို ခေါ်သုံးပါတယ်။ Person.CompareAge ဆိုပြီး class name ဖြစ်တဲ့ Person အနေနဲ့ တိုက်ရိုက်ခေါ်သုံးတာ သတိထားမိမှာပါ။ ကျန်တဲ့အဆင့်တွေကတော့ ဘယ်သူ အသက်ပိုကြီးတယ်ဆိုတာကို messagebox သုံးပြီးတော့ ပြတာပါ။
အနှစ်ချုပ်အနေနဲ့ shared ကို မှတ်ထားချင်တယ်ဆိုရင်တော့ …
- shared ဆိုတာ class က ပိုင်ဆိုင်တယ်။
- shared ကို class တွေကြားမှာ မျှဝေသုံးစွဲကြတယ်။
- shared ကို ခေါ်သုံးချင်ရင် object(instance) တွေ ကြေငြာပြီးသုံးစရာမလိုဘူး။ class name နဲ့ တိုက်ရိုက်ခေါ်သုံးလို့ရတယ်။
Facebook comments:



