NAVIGATION:

 

SUPPORTERS:

FlashComponents
  Flash Templates
  Flash Gallery
  Flash Slideshow
  Flash Menu
  Flash Design
  Flash Video
  User Interface

 

FOLLOW:

RSS it down! Twitter it up! Facebook it like a fiend!

 

 

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.


I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 

Cheers!

Kirupa Chinnathambi
facebook | twitter

 

1 | 2

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components Flash effects. Art without coding.
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Citrus Engine: Flash platformer and sidescrolling game engine
Learn how to advertise on kirupa.com

cdn
content delivery network (cdn)