21 February 2010
C# Art
Assalamulaikum wbt..
Alhamdulliah, syukur ke hadrat ilahi kerana dengan limpah kurnia nye aku bleh ar bernafas lgi atas muka bumi nie selaku insan bernama mohd yazid..
Ok, skg nie rmai .Net programmer y masih beginner mcm aku still wat programing dlm cara y konvensional or tipikal. Sometimes its good for readability (senag nk debug), tp prasan or x, ia bleh menyebabkan kurangnye reliability in term of efficiency, performance, memory and battery consumption. Bnde nih jarang org tgk, y penting projek siap dh la kn. Tp aku sgt interested kat bnde2 cmni..sbb nye..itulah the arts of programming..tul x?
"Get and Set functionality"
First, nk explain skit sal Get n Set nie. Mcm bese, Get adalah bile kite nk dptkan value from that particular object instance, n Set bile kite nk initialize or assign value. Nk lebih clear, aku bg contoh kat bwah nie:
Contoh 1:
private string myname;
public string GetMyName()
{
return myname;
}
public string SetMyName(string name)
{
myname = name;
}
Contoh 2:
public string myname {get; set; }
Nampak obvious kn.. 2 bnde y sama, tp 1 lagi pnjang, n 1 lagi pendek jaa code nye. 1 line je..Its up to us as a programmer which one to pick. Tp persoalannye, bile masa nk gune contoh 1, n bile plak nk gune contoh 2?? Simple, gune contoh 1 bile melibatkan variable y penting, protected, or shared with other crucial function, class or object. Sbbnye, most of the time, hackers bleh hack n manipulate ape2 value bile variable di declare as a public. Solutionnye, declaration variable adalah private or protected, but kite wat another public function untuk access variable tersebut (contoh 1). Semua bnde ada drawback rite. Drawback bnde nie adalah ia memerlukan another memory space utk pass by value dlm parameter function tersebut dan code die agak pnjag sikit.Kalu variable tue xbrape important (most of the time) cam perkataan dlm textbox, penggunaan Get n Set (contoh 2) amat disyorkan oleh aku.Sbb nye, ia simple, menjimatkan ruang (1 line je) n by default, bnde nie gune pass by referrence (not by value) which means its variable would share exactly the same memory space with function call. Dengan kata lain, korang akn jimat 1 memory space n lebih efficient.
"Quick User Control Initialization"
Ok bnde nie pening sikit.Sila baca dengan teliti. User Control aku dh explain kat post2 y lepas. Jd sila bace tu dlu k. First, let say aku wat 1 user control (MyUserControl) y ader textbox (txtName) n label (lblName)..Then cam bese, cm programmer .Net y laen buat bile nk initialize User Control adalah seperti berikut:
Declaration:
Public partial class MyUserControl : usercontrol
{
Public MyUserControl()
{
InitializeComponent();
}
}
Initialization:
MyUserControl myUC = new MyUserControl();
myUC.lblName.text = "Nama: ";
myUC.txtName.text = "Mohd Yazid Mohd Jaafar";
myUC.Width = 100;
myUC.Height = 150;
This is tipical way y bese kite (n aku jgak) wat bile wat C# programming. Byk line diperlukan juz nk initialize 1 user control jer. Utk bg korang nmpak, aku bg contoh laen y lebih jimat n efficient:
Declaration:
Public partial class MyUserControl : usercontrol
{
Public MyUserControl()
{
InitializeComponent();
}
Public MyUserControl(string names, string labels, int height, int width) //Override Constructor
{
InitializeComponent();
this.txtName.text = names;
this.lblName = labels;
this.Height = height;
this.Width = width;
}
}
Initialization:
MyUserControl myUC = new MyUserControl("Mohd Yazid", "Nama: ", 100, 150);
Simple, 1 line jer utk initialization. nie la die ape y aku explain dlm post2 y lepas sal separation of class for efficiency and reliability. Xperlu byk2 code dlm main class. Xelok tamak2 nie. Share sikit code y belambak tue kat class or object y lain. Nmpak kemas, n senang nk debug. Kadang aku pelik gak. Sometimes org bangga bile kate code die dh smpai 1000 lines. Tp pd aku, lagi bagus kalu kite bleh wat bende y sama tp hanya menggunakan xsampai 500 lines pun. Tu mknanye die xreti nk utilize Object-Oriented Programming..
"Conclusion"
Orang kdang2 kate, "Buat cmni pn same gak aper..". Mmg la same bro..Its ok if korang wat hanya utk siapkan assignment or project y duedate tinggal esok ja. Tp cube byagkan kalu korang dh keje as a programmer, wat 1 apps but in the end, customer complain y apps korang nie pesal slow sgt, hang laa..itu laa..ini laaa..Kat situ la dtg nye code efficiency, memory consumption n bla bla bla..Prasan or x, Microsoft Office Word Beta 2010 tue bru jer mkan 70 - 80 KB dlm task manager korang. Software punye la besar, ader mcm2 tp ringan jer..Agak2 cmana programmer Microsoft wat mende nih..
Ape pun, utk capai 1 mission, ader byk cara. Bile ader bug, ader 1001 solution utk nye. For me, I dont care if I got A, B, C or even D for my project. But the thing is, I want the KNOWLEDGE on top of it..
Gudluck guys..
p/s: Sumer code ditulis on the spot kat blog.Kalu ader silap, hrp dimaafi
.
Labels:
C#,
programming
Subscribe to:
Post Comments (Atom)
This comment has been removed by the author.
ReplyDeletetahniah zid..=)
ReplyDeletei'm really proud of u..