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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

underscore prefix 1

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
I few times when looking at code on the net I have seen objects, variables etc prefixed by an underscore. But I've never seen any explanation for the reason for this. What is the reason for doing this?


Thanks,

lfc77
 
Private variables are often prefixed by underscore. The reason for it is when you write a property than you need private variable and public property. For example
private int _MaxLength = 0;
public int MaxLength
{
.
.
.
}

Programmers that came from C++ often use a m_ as a prefix.
 
It's a convention that indicates private variables (like bobisto says). The other main convention is to use "m_" as a prefix.

I prefer the underscore by itself, as they will sort to the top of any intellisense list, and other developers will know not to call them. (they won't be lumped in between "ListData" and "NoodleWithData" in the list).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I personally use them dependant on scope. For example:

Code:
class foo
{
    private string m_sFoo = String.Empty;
    
    public string Foo
    { get { return this.m_sFoo; } set { this.m_sFoo = value; } }

    public string SayHello
    {
        string _sHello = "Hello " + m_sFoo;
    }
}

I go with this method for two reasons:

1) It allows me to easily tell at a glance the scope of the variable (a "_" means method-level, a "m_" means class-level)
2) I also use the Hungarian notation identifier after the "_" so I can easily identify what data type I'm working with ("s" for string, "b" for boolean, "i" for integer, etc.)

Although this is my personal preference (and no, my team at work doesn't completely implement this :p), you'll find arguments against it too:
I think it all boils down to personal preference, what you're most comfortable with. If you're in a team environment, make sure that everyone on the team uses the same notation standard. Otherwise, code can end up pretty ugly (if it's not already);)

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top