Introduction to C# part 2 : property

อ่า ห่างหายกันไปนานเลยนะคับ สำหรับตอนต่อไปของ basic C# เนื่องจากว่าเพิ่งเปิดเรียนใหม่นะคับ แล้วงานก็พรุ่งพรวดกันเข้ามาเลยทีเดียว

ในคราวนี้แทนที่ผมจะพูดถึง Data Types ผมจะไปพูดถึงสิ่งที่มีใหม่ใน C# แทนนะครับ เนื่องจาก Data Types จะเหมือนๆกับในหลายๆภาษาอื่นๆ โดยมีเพิ่มเติมขึ้นมาอยู่บ้าง

ในวันนี้เราจะพูดถึงเรื่อง propertyกันนะคับ

คุณเคยรู้สึกไหมว่า ตอนที่คุณต้องการ access ใน field ที่คุณต้องการนั้น ต้องมานั่งสร้าง method get หรือ set ให้มัน

สำหรับในภาษา C# นั้น สามารถทำให้เราสามารถเข้าถึงฟิลด์เรานั้นได้อย่างสะดวกสบายเลยล่ะคับ โดยใช้คุณสมบัติที่เรียกว่า property

ซึ่งเขียนสั้นกว่าเดิม แต่มีกลไกคล้ายแบบเดิม

using System;
using System.Text;

 class House
    {
        public int build = 1;
        private string prpColor = "Green";
        public string color
        {
            get { return prpColor; }
            set { prpColor = value; }
        }
    }

    class Start
    {
        static void Main(string[] args)
        {
            House sansook = new House();
            sansook.color = "white";
            Console.WriteLine(sansook.build + " : " + sansook.color);
            Console.ReadLine();
    }

เมื่อเราใช้proptertyนะคับ เราก็จะสามารถรักษา encapsulation ไว้ได้ แล้วก็ ช่วยให้การเขียนสะดวกขึ้นอีกด้วย

public string color
{
 get { return prpColor; }
 set { prpColor = value; }
}
get จะเปนส่วนที่ไว้ใช้ return ค่านะครับ เหมือนเป็น method pubic String get_color()
set จะเปนส่วทนี่ไว้ใช้ รับค่านะครับ ผ่านทาง parameter ที่ชื่อว่า value เหมือนเปน method public void set_color(String x)

ในวันนี้ก้อจบลงแต่เพียงเท่านี้นะคับ ถ้าสงสัยหรือมีอะไรเพิ่มเติมคอมเมนได้นะครับ แล้วผมจะมาเพิ่มเติมต่อในภายหลังครับ

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (15) | RSS


Comments

June 1. 2009 19:54

cmmadnat

Very interesting. Keep it coming Q!

cmmadnat

June 1. 2009 20:56

Nat

Fight fight na Qvy  ^^

Nat

August 10. 2009 05:23

payday loans

amazing stuff thanx Smile

payday loans

August 12. 2009 05:22

cash loans

Such a usefule blog�wow !!!!

cash loans

August 27. 2009 20:08

bad credit loans

my God, i thought you were going to chip in with some decisive insght at the end there, not leave it with �we leave it to you to decide�.

bad credit loans

August 27. 2009 23:16

bad credit loans

What is captcha code?, pls provide me captcha code codes or plugin, Thanks in advance.

bad credit loans

August 27. 2009 23:18

bad credit loans

thanks !! very helpful post!

bad credit loans

September 16. 2009 23:02

payday loan

Hello Guru, what tempt you to post an article. This article was exceedingly fascinating, especially since I was searching for thoughts on this subject last Thursday.

payday loan

September 17. 2009 01:12

payday loan

I don�t usually reply to posts but I will in this case. I�ve been getting this very same problem with a new WordPress installation of mine. I�ve spent weeks calibrating and getting it ready when all of a sudden� I cannot delete any content. It�s a workaround that, although isn�t perfect, does the trick so thanks! I really hope this problem gets solved properly asap.

payday loan

October 31. 2009 03:44

easy cash loans

Interesting post

easy cash loans

November 5. 2009 01:54

paydayloans

Yea nice Work !Laughing

paydayloans

November 9. 2009 01:49

online personal loans

Just try to smile for about 2-3 mins then you can get back to work

online personal loans

November 24. 2009 18:36

payday loans

Nice post . keep up the good work

payday loans

December 9. 2009 11:11

cash loans

I like what I see. keep it going

cash loans

January 2. 2010 00:40

fast payday loans

I guess there's always an easier way ...

fast payday loans

Add comment




biuquote
  • Comment
  • Preview
Loading



 

Tag Cloud