MINI SUPPORTERS:

 

 

Mixing Data Types using Structs - Page 2
       by kirupa  |  1 January 2007

In the previous page, you learned the basics of how to use structs as well as some important things to keep in mind about them. In this page, I will expand on the earlier example by using Properties to make the code more maintainable.

Using Properties
In my simple example, you access the public fields for your struct instance directly. While that is an acceptable way to store and retrieve data, they limit extensibility. When writing programs, you want to try your best to add/change functionality without breaking existing code.

When you access public fields directly, you are simply retrieving a stored value. If you later decide to perform some sort or processing instead of retrieving the raw data, for example you want the age in days instead of years, such a change may break parts of our application that depended strictly on the earlier implementation. For a simple example such as what you see here, re-writing some code is painless. When you are working on a larger application, such bug fixes can be time-consuming.

One solution to that problem is by bypassing public fields and using Properties. Before I continue on, let me provide you the code for our example using Properties:

public struct Person
{
private string firstName;
private string lastName;
private int age;
 
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Person homer = new Person();
homer.FirstName = "Homer";
homer.LastName = "Simpson";
homer.Age = 36;
 
Console.WriteLine("Person's first name is {0}", homer.FirstName);
}
}

The functionality between my simple example and what is shown above is the same. The difference is that while I can easily extend my example using the Properties approach, it will take some code rewriting to do the same in the simpler example using public fields.

For example, the following is something that cannot be done using the public field approach:

public int Age
{
get
{
if (age > 30)
{
return age * 2;
}
else
{
return age / 2;
}
}
set
{
age = value;
}
}

What is the great is that when you are using Properties, you don't modify how you set or  access any data from the struct instance. I would set the age property as homer.Age = 36, and I would get the age by accessing homer.Age without the assignment ( = ) operator. The get/set keywords take care of assigning or displaying values without you doing anything differently.

The main takeaway message of this section is that you should use Properties when you can, for even though it has a higher initial cost, the benefits you gain from improved extensibility and controlling access to an object's internal state (encapsulation) is definitely worth it.


Need Help?

If you have questions, need some assistance on this topic, or just want to chat - please drop by our friendly forums and post your question. There are a lot of knowledgeable and witty people who would be happy to help you out. Plus, we have a large collection of smileys you can use

Share

Did you enjoy reading this and found it useful? If so, please share it with your friends:

If you didn't like it, I always like to hear how I can do better next time. Please feel free to contact me directly at kirupa[at]kirupa.com.

Cheers!

Kirupa Chinnathambi
about | facebook | twitter

 

1 | 2

SUPPORTERS:

cloud storage
cloud storage
kirupa.com's fast and reliable hosting provided by Media Temple. Creative web apps. Make your own free flash banners and photo slideshows.
HTML5 CSS3 Mobile Gallery for iPhone, iPad Flash effects. Art without coding.
Flipping Book - page flip flash component. Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery
X-Platform Application Development for Flash Free Flash Components Download - XML Templates, Players and Galleries.

two computer monitors

US Direct

Learn how to advertise on kirupa.com  
 
SHARE:



MINI SUPPORTERS: