set and get
public bool m_MyFlag
{
set { m_flag = value };
get { return m_flag };
}
protected bool m_flag;
Need a suggestion for class's property:
is this the only way to use set & get?
I mean, I don't like to use m_flag in the class's functions and m_MyFlag outside of the class.
Do I really need to have two identifiers (m_MyFlag and m_flag)?
How do you behave in these situations?
# 1 Re: set and get
In general, the Get Set property should be well-describing. for example.
public bool FlagforUpdate
{
set { m_bUpdateFlag = value };
get { return m_bUpdateFlag };
}
private bool m_bUpdateFlag ;
# 2 Re: set and get
isn't it just as I did? You have two identifiers for the same property: FlagforUpdate and m_bUpdateFlag. This is what I wanted to avoid.
# 3 Re: set and get
well, the only solution will be marking your boolean value as public
public bool m_bUpdateFlag = false;
and then simple use it outside the class as:
MyClass m_myClass = new MyClass();
if(m_myClass.m_bUpdateFlag)
{
...
}
I think that set/get is provided to ease the coding and sharing variable.
# 4 Re: set and get
Properties come into their own particularly when creating UI components. Only properties will appear in the Property Grid.
Properties are really methods so validation checks or event notification can also be added.
Also you do not need to provide both a get and set so it's easier to control access.
For debugging it's also useful being able to put a breakpoint on the property to see who's modifying your object.
etc. etc.
Norfy at 2007-11-9 1:39:39 >

# 5 Re: set and get
Thank you for your explenations.
But what if I don't want class's users to set m_flag:
public bool m_MyFlag
{
get { return m_flag };
}
protected bool m_flag;Would you write it like that (still having two identifiers for the same information)?
In this situation you use m_flag inside of class's functions and m_MyFlag outside.
Don't you agree with me when I say that this leads to a little bit of confusion?
# 6 Re: set and get
But what if I don't want class's users to set m_flag: etc
Yes.
Probably "yes" to the other question regarding confusion.
Personally I don't use the 'm_' notation, so all variables with a lowercase letter must be member variables.
Norfy at 2007-11-9 1:41:38 >

# 7 Re: set and get
'm_' notation or not you have two identifiers for the same information, don't you? Would you write it like I did?
# 8 Re: set and get
"'m_' notation or not you have two identifiers for the same information, don't you?"
Yes. This does effectively mean doubling the amount of identifiers for a class.
(I prefer this notation than having to provide get<Property>() and set<Property>(), as in Java)
"Would you write it like I did?"
Almost :) Small typo:
get { return m_flag;}
otherwise, yes.
Norfy at 2007-11-9 1:43:42 >

# 9 Re: set and get
public bool m_MyFlag
{
get { return m_flag };
}
protected bool m_flag;
m_flag is the variable that holds the value.
m_MyFlag is the public interface to this variable.
A class's properties are for use by consumer's of the class. Hence from that perspective, the consumers of the class see only one identifier i.e. m_MyFlag.
Ofcourse, as the developer of the class, from within the class you seem to have 2 identifiers. i.e. the actual variable that holds the information and the public interface i.e. the property.
While writing a class, I try not to use the property within the class, but use the variable directly. Using the property could get you into an unnecessary mess . For example -
public bool m_MyFlag
{
get { return m_MyFlag; } //Note this line
set { m_flag = value;}
}
protected bool m_flag;
Here, the get section will cause an indefinite loop, till you run out of memory...
Hope this answers the question
-Satish
# 10 Re: set and get
Also, I concur with Andy Tacker's first post...
Consumers of your class will use the property by its name, so a well described name is desireable.
-Satish
# 11 Re: set and get
:p You fusspot, Norfy :D :D thank you.
and thank you to Satish as well. I know how set/get work, it's just that I wanted to know if you think that this is a good way to write C# code. Because it looks not very nice, to me. I'm not talking of variable names, these were just examples, I'm talking of having the two identifiers.
# 12 Re: set and get
I wanted to know if you think that this is a good way to write C# code.Yes, this is the way to write C# code. It may look messy to the class designer, but it is neat for the class user(s).
# 13 Re: set and get
and keeping in mind usage of variable identifiers, for example in your case, writing
bool m_flag;
does not provide the information about the type. so, writing m_bFlag would be more self-explantory.
and yes, as Acephalus said, this is how C# is coded.
# 14 Re: set and get
Hi Andy,
do you find this notation is still useful in VS. NET? The IDE always tells me what operations I have available for any given object.
I liked your earlier answer with respect to using 'well describing' names.
Norfy at 2007-11-9 1:49:54 >

# 15 Re: set and get
Norfy,
I caught this notation Virus ever since i touched C, and moved on to VC and to VC++. can't get rid of it :(.
but as for me, the notation serves good.