473,320 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A Question of Style: Accessor Methods

Hi,

I'm not happy with the way in which I use accessors and was wondering
if there is a better way of defining them. Take the following example
-

class MyClass
{
// prepended to version names appearing in version combo box
private static string versionPrefix;

// the current version selected by user
public currentVersionName = null;

public MyClass()
{
versionPrefix = GetVersionPrefix();
}

...

// prepended to version names appearing in version combo box
public static string VersionPrefix
{
get
{
return versionPrefix;
}
}

...
}
comboBoxVersion.SelectedIndex =
comboBoxVersion.FindStringExact(MClass.VersionPref ix +
myClass.CurrentVersionName);

The problem is the amount of lines versionPrefix and its accessor
takes up within the class definition. What styles do you guys use?
Also, I don't like how the definition and accessor method is split up
and I feel like I have to repeat my comments. It feels to my that
get{} is a property of versionPrefix (you can get me but not set me),
and therefore belongs with its declaration in the same way that static
does. Perhaps I'd prefer to do something like this before my
constructor, but perhaps its not good practice -

// prepended to version names appearing in version combo box
private static string versionPrefix = "Version Name: ";
public static string VersionPrefix { get { return
versionPrefix; }}

Thanks for your suggestions,

Barry.

Apr 30 '07 #1
1 1355
Re breaking up the field and property; I agree: stick them next to
eachother. This also removes the need to duplicate the comment, which
would be better as an xml comment for intellisense.

Re the number of lines... then don't use as many? Event sticking with
Allman you can reduce the line count significantly...
If something is genuinely readonly, then I suggest marking it as such
in the field definition; if you had done so you would have noticed
that you are setting the static field in the instance ctor.

Finally, regions may help (although if there are more than a members
in each I tend to use partial classes to split the instance and static
parts into a MyClass.instance.cs and MyClass.static.cs)

class MyClass
{
#region instance members
public MyClass() {} // default instance ctor
#endregion

#region static members
static MyClass() // initialise the (static) version prefix
{
versionPrefix = GetVersionPrefix();
}
private readonly static string versionPrefix;
/// <summary>Prepended to version names appearing in version combo
box</summary>
public static string VersionPrefix
{
get { return versionPrefix; }
}
#endregion
}

Marc
Apr 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: mirandacascade | last post by:
When I look at how classes are set up in other languages (e.g. C++), I often observe the following patterns: 1) for each data member, the class will have an accessor member function (a...
2
by: Todd A. Anderson | last post by:
I've inherited two "C++" code bases that I have to tie together and both of them make frequest use of public member variables in spite of all the information saying this limits flexibility. Well,...
22
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is...
3
by: LuCk | last post by:
Can someone explain what these really are for example: ---------------------------------------------------------- void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; }; ...
6
by: Jason Shohet | last post by:
I have a class with protected variables and some accessor methods, , get, set ... Maybe I have a brain blockage today but I'm thinking, why not just make those variables public. After all,...
7
by: Tenacious | last post by:
I have been programming using C# for over a year now and I still wonder what is the importance of using accessor methods when the property is read-write. It seems easier to just make it a public...
8
by: AAJ | last post by:
Hi all I would like to have a class that can set/return values of different datatype via a single accessor, i.e. overload the accessor i.e. something like DateTime m_DateValue; string...
8
by: Tim Sprout | last post by:
Why is it considerd best practice to use Properties rather than Get and Set accessor methods? -Tim Sprout
2
by: rbjorkquist | last post by:
This is my first attempt at writing/using web services, so any and all comments will be greatly appreciated. With that said, I am also by no means saying this is the correct either. I have...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.