473,548 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class member addressing

MM
Hi,

I was taught to use this type of class/member construction (some years
ago I might add) -

class someclass
{
private datatype data1;
private datatype data2;

public someclass( datatype data1, datatype data2 etc)
{
this.data1 = data1;
this.data2 = data2;
etc...

but alot of modern texts seem to decry the use of 'this' to
prefix/access class memebers and don't even seem to bother with explicit
'private'. What's the 'norm' these days? Thanks!
Nov 17 '05 #1
9 1276
"MM" <mm@newsgroups. com> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

I was taught to use this type of class/member construction (some years ago
I might add) -

class someclass
{
private datatype data1;
private datatype data2;

public someclass( datatype data1, datatype data2 etc)
{
this.data1 = data1;
this.data2 = data2;
etc...

but alot of modern texts seem to decry the use of 'this' to prefix/access
class memebers and don't even seem to bother with explicit 'private'.
What's the 'norm' these days? Thanks!


I find this clearer:

private datatype _data1;
private datatype _data2;

Michael
Nov 17 '05 #2
Hi MM,
I prefer, like Michael C, to put an underscore next to private member
variables to aid readability. I also personally like adding "this" inforont
of variables but it is just personal preference I think it makes code more
clear, others may disagree. I think that when coding it is better to be
explicit, such as declaring variables private as this also aids readability.

Microsoft has some coding guidlelines for writing class libararies, which is
worth reading. Check out
http://msdn.microsoft.com/library/de...guidelines.asp

Hope that helps
Mark R Dawson

"MM" wrote:
Hi,

I was taught to use this type of class/member construction (some years
ago I might add) -

class someclass
{
private datatype data1;
private datatype data2;

public someclass( datatype data1, datatype data2 etc)
{
this.data1 = data1;
this.data2 = data2;
etc...

but alot of modern texts seem to decry the use of 'this' to
prefix/access class memebers and don't even seem to bother with explicit
'private'. What's the 'norm' these days? Thanks!

Nov 17 '05 #3
MM <mm@newsgroups. com> wrote:
Hi,

I was taught to use this type of class/member construction (some years
ago I might add) -

class someclass
{
private datatype data1;
private datatype data2;

public someclass( datatype data1, datatype data2 etc)
{
this.data1 = data1;
this.data2 = data2;
etc...

but alot of modern texts seem to decry the use of 'this' to
prefix/access class memebers
It's a personal choice. At work I use "m_variableName " due to the
coding standards there, but at home I write code like the above. So
long as the variables stay private, it makes no odds.
and don't even seem to bother with explicit
'private'. What's the 'norm' these days? Thanks!


Whether you explicitly state "private" or not is even more of a
personal choice - it doesn't make *any* difference to the generated
code. Personally I don't bother with it - because the defaults in C#
are so well chosen (always the most restrictive access available) I
prefer to have it highlighted to me that I've made something *less*
restrictive.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Hi, MM

Some of this can be addressed straight from Microsofts .Net guidelines...

http://msdn.microsoft.com/library/de...Guidelines.asp

I notice that some books, and web articles, use conventions which contradict
the Microsoft guidelines. I don't know when the guidelines were first
published, but I think that it would make life easier for the whole C#
community if people were to always use them in new code, if they have a
choice to do so.

On to your questions...
class someclass
{
private datatype data1;
You should definitely use Pascal case for class names. This is almost
universal. ie. "class SomeClass". Similarly for any user defined types, ie
"DataType"

I personally have a bit of sypathy for the _ prefix to class members, and
some good authors use it, however the conventions say no, and most C# coders
now avoid it.

"Do not use Hungarian notation for field names. Good names describe
semantics, not type.

Do not apply a prefix to field names or static field names. Specifically, do
not apply a prefix to a field name to distinguish between static and
nonstatic fields. For example, applying a g_ or s_ prefix is incorrect. "
public someclass( datatype data1, datatype data2 etc)
{
this.data1 = data1;
this.data2 = data2;
That's exactly how most C# code does it (except for the upper-low case
anomolies I've pointed out).
but alot of modern texts seem to decry the use of 'this' to
prefix/access class memebers
They are quite correct. One rarely sees "this" in good code, except when it
is needed to disambiguate identitiers (as in the constructor above).
and don't even seem to bother with explicit 'private'.
What's the 'norm' these days? Thanks!


Again, I agree with this, and I agree with Jon's argument for it.

Regards,

Javaman
Nov 17 '05 #5
I sort of disagree with the take in "this". this is implicit for members so
one need not type it *except* when using an IDE with intellisense where
typeing "this.som" is way easier than typeing
"SomeLongMethod NameThatsEasyTo Misspell". Having this. in a statement is not
hard to read and adds no overhead for the compiler.

Even though it's discouraged by the style-police I wholeheartedly agree with
the prefix of an underbar in private members because a program written in C#
that doesn't make use of the underbar for private members and relies only on
capitalization for the distinction between the member and the accessor
property is a complete pig to translate to VB.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Javaman59" <Ja*******@disc ussions.microso ft.com> wrote in message
news:FB******** *************** ***********@mic rosoft.com...
Hi, MM

Some of this can be addressed straight from Microsofts .Net guidelines...

http://msdn.microsoft.com/library/de...Guidelines.asp

I notice that some books, and web articles, use conventions which
contradict
the Microsoft guidelines. I don't know when the guidelines were first
published, but I think that it would make life easier for the whole C#
community if people were to always use them in new code, if they have a
choice to do so.

On to your questions...
class someclass
{
private datatype data1;


You should definitely use Pascal case for class names. This is almost
universal. ie. "class SomeClass". Similarly for any user defined types, ie
"DataType"

I personally have a bit of sypathy for the _ prefix to class members, and
some good authors use it, however the conventions say no, and most C#
coders
now avoid it.

"Do not use Hungarian notation for field names. Good names describe
semantics, not type.

Do not apply a prefix to field names or static field names. Specifically,
do
not apply a prefix to a field name to distinguish between static and
nonstatic fields. For example, applying a g_ or s_ prefix is incorrect. "
public someclass( datatype data1, datatype data2 etc)
{
this.data1 = data1;
this.data2 = data2;


That's exactly how most C# code does it (except for the upper-low case
anomolies I've pointed out).
but alot of modern texts seem to decry the use of 'this' to
prefix/access class memebers


They are quite correct. One rarely sees "this" in good code, except when
it
is needed to disambiguate identitiers (as in the constructor above).
and don't even seem to bother with explicit 'private'.
What's the 'norm' these days? Thanks!


Again, I agree with this, and I agree with Jon's argument for it.

Regards,

Javaman

Nov 17 '05 #6
"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:u4******** ******@TK2MSFTN GP12.phx.gbl...
I sort of disagree with the take in "this". this is implicit for members so
one need not type it *except* when using an IDE with intellisense where
typeing "this.som" is way easier than typeing
"SomeLongMetho dNameThatsEasyT oMisspell". Having this. in a statement is not
hard to read and adds no overhead for the compiler.
Try

som ctrl space

I must say I'm suprised at the number of developers who don't know this
extremely useful shortcut :-) I must use it, without exaggeration, 200 times
a day when coding.
Even though it's discouraged by the style-police I wholeheartedly agree
with the prefix of an underbar in private members because a program
written in C# that doesn't make use of the underbar for private members
and relies only on capitalization for the distinction between the member
and the accessor property is a complete pig to translate to VB.


Plus it eliminates conflicts (and hence possible bugs) between variable
name. When naming a private variable or function parameter there is zero
chance it will conflict with a class variable. I believe the MS standard
used to have the underscore and at least some of the private variables in
the framework have it.

Michael
Nov 17 '05 #7
"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:u4******** ******@TK2MSFTN GP12.phx.gbl...
I sort of disagree with the take in "this". this is implicit for members so
one need not type it *except* when using an IDE with intellisense where
typeing "this.som" is way easier than typeing
"SomeLongMetho dNameThatsEasyT oMisspell". Having this. in a statement is not
hard to read and adds no overhead for the compiler.
Try

som ctrl space

I must say I'm suprised at the number of developers who don't know this
extremely useful shortcut :-) I must use it, without exaggeration, 200 times
a day when coding.
Even though it's discouraged by the style-police I wholeheartedly agree
with the prefix of an underbar in private members because a program
written in C# that doesn't make use of the underbar for private members
and relies only on capitalization for the distinction between the member
and the accessor property is a complete pig to translate to VB.


Plus it eliminates conflicts (and hence possible bugs) between variable
name. When naming a private variable or function parameter there is zero
chance it will conflict with a class variable. I believe the MS standard
used to have the underscore and at least some of the private variables in
the framework have it.

Michael
Nov 17 '05 #8
MM
Thanks to all you guys for the advice - think I'll run with _var and
drop 'this' (atleast outside of the constructor). Still undecided about
the access modifier but will probably keep as I'm abit pendantic. Thanks
to Michael C for pointing out the intellisense hotkey thing - one of the
prime reasons I've kept with 'this'. Cheers, m.
Nov 17 '05 #9
MM
Thanks to all you guys for the advice - think I'll run with _var and
drop 'this' (atleast outside of the constructor). Still undecided about
the access modifier but will probably keep as I'm abit pendantic. Thanks
to Michael C for pointing out the intellisense hotkey thing - one of the
prime reasons I've kept with 'this'. Cheers, m.
Nov 17 '05 #10

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

Similar topics

66
6568
by: Mike Stenzler | last post by:
I am new to Template programming and I would like to create an array of user-defined class objects using MFC CArray. Normally I would use linked list processing - create an object class and then an object-list class, but in this instance array processing would be more efficient. However I need to use arrays that can dynamically shrink and grow...
4
2482
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
9
2298
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
14
3117
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are very unhappy with the idea and the compiler refuses to play ball with any of the attempts I have made to build such a structure. There is no...
0
1318
by: gerritmitchell | last post by:
Hi, I have a situation where I need to send a SOAP message from a receiver through multiple intermediaries and then to an ultimate receiver. The intial sender will tell the intermediary where to send the message to next within the service chain. SOAP targeted headers and SOAP roles seem to suppor this quite well. My question is can this...
15
2380
by: Victor Bazarov | last post by:
Hello, Take a look at this program: ----------------------------------- class B { B(const B&); B& operator=(const B&); public: B(int);
1
4084
by: =?Utf-8?B?dWx0cmFuZXQ=?= | last post by:
We have a client that uses .Net that needs to work against our Java (xfire) based WS. My question is: how can a .Net (C#) WS client be configured to not send WS-Addressing headers? The client in particular mentions having tried WSE 2.0, and WSE 3.0, and says there is no way to turn off WS-Addressing in a WS client, using those frameworks. I...
20
4011
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. ...
36
2008
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are there any other no-overhead ways that a contained class can access its container? The obvious choice of passing (a pointer or a reference to the...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.