Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pros and Cons for using structures

Status
Not open for further replies.

CodingIsFun

Programmer
Apr 9, 2004
134
US
Hi all experts

We have been having internal meetings about coding standards and are trying to decipher some best practices with regards to development.

We have 2 sides on this debate:

Using structures to hold data or make individual variables with sets/gets and overloaded functions within a class.

If anyone has something to add please put it in, Im very interested in what everyone else thinks about this.


thanks in advance...
 
They have 2 very different purposes.

I always consider a struct as being very raw. you are just dealing with a set of variables and nothing more. this has its purposes.

I tend to use classes (a.k.a beans) which give you a few more options. For instance:

When I set a variable such as color - I might want to create a brush or a pen for drawing at that time. This is when the setters and getters are handy.

public Color DrawingColor
{
get
{
return drawingbrush.Color;
}
set
{
drawingbrush = new SolidBrush(value);
}
}

I then also like the availablity to have "self-populating" classes which you can't really get with a struct. Overloading the constructor is always nice - but also to have a static method that will generate an instance of itself.

DrawingTool custompen = DrawingTool.SelectPen(Color.Blue);


Now adays, I tend to only use structs if I'm interacting with C++ components. Afterall, I can set the variables of a class the same way I can with a struct if I make them public.
 
Yup. I only use structs when I'm dealing with legacy data structures. Any new code is written with classes, using getters/setters, instance/static methods, constructors, etc.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top