Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

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
kirupaBlog

 

1 | 2


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com