Search This Blog

2009-12-01

Tutorial 9:Properties

Properties allow you to access objects state with field-like syntax.
Accessing Class Fields With Properties.
using System;

public class Customer
{
private int m_id = -1;

public int ID
{
get
{
return m_id;
}
set
{
m_id = value;
}
}

private string m_name = string.Empty;

public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
}

public class CustomerManagerWithProperties
{
public static void Main()
{
Customer cust = new Customer();

cust.ID = 1;
cust.Name = "Amelio Rosales";

Console.WriteLine(
"ID: {0}, Name: {1}",
cust.ID,
cust.Name);

Console.ReadKey();
}
}
It shows how to create and use a property. The Customer class has the ID and Name property implementations. There are also private fields named m_id and m_name; which ID and Name, respectively, encapsulate. Each property has two accessors, get and set. The accessor returns the value of a field. The set accessor sets the value of a field with the contents of value, which is the value being assigned by calling code. The value shown in the accessor is a C# reserved word.
When setting a property, just assign a value to the property as if it were a field. The CustomerManagerWithProperties class uses the ID and Name properties in the Customer class. The first line of Main instantiates a Customer object named cust. Next the value of the m_id and m_name fields of cust are set by using the ID and Name properties.
To read from a property, use the property as if it were a field. Console.WriteLine prints the value of the m_id and m_name fields of cust. It does this by calling the ID and Name properties of cust.
This was a read/write property, but you can also create read-only properties

Read-Only Properties
using System;

public class Customer
{
private int m_id = -1;
private string m_name = string.Empty;

public Customer(int id, string name)
{
m_id = id;
m_name = name;
}

public int ID
{
get
{
return m_id;
}
}

public string Name
{
get
{
return m_name;
}
}
}

public class ReadOnlyCustomerManager
{
public static void Main()
{
Customer cust = new Customer(1, "Amelio Rosales");

Console.WriteLine(
"ID: {0}, Name: {1}",
cust.ID,
cust.Name);

Console.ReadKey();
}
}

Creating a Write-Only Property
using System;

public class Customer
{
private int m_id = -1;

public int ID
{
set
{
m_id = value;
}
}

private string m_name = string.Empty;

public string Name
{
set
{
m_name = value;
}
}

public void DisplayCustomerData()
{
Console.WriteLine("ID: {0}, Name: {1}", m_id, m_name);
}
}

public class WriteOnlyCustomerManager
{
public static void Main()
{
Customer cust = new Customer();

cust.ID = 1;
cust.Name = "Amelio Rosales";

cust.DisplayCustomerData();

Console.ReadKey();
}
}

Auto-Impemented Properties
using System;

public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}

public class AutoImplementedCustomerManager
{
static void Main()
{
Customer cust = new Customer();

cust.ID = 1;
cust.Name = "Amelio Rosales";

Console.WriteLine(
"ID: {0}, Name: {1}",
cust.ID,
cust.Name);

Console.ReadKey();
}
}

No comments: