473,480 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create 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 1274
"MM" <mm@newsgroups.com> wrote in message
news:OF**************@TK2MSFTNGP12.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.com>
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
"SomeLongMethodNameThatsEasyToMisspell". 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*******@discussions.microsoft.com> wrote in message
news:FB**********************************@microsof t.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@_spamkiller_bobpowell.net> wrote in message
news:u4**************@TK2MSFTNGP12.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
"SomeLongMethodNameThatsEasyToMisspell". 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@_spamkiller_bobpowell.net> wrote in message
news:u4**************@TK2MSFTNGP12.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
"SomeLongMethodNameThatsEasyToMisspell". 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
6553
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...
4
2469
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
2283
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...
14
3110
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...
0
1309
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...
15
2373
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
4075
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...
20
4000
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...
36
1993
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...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7102
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...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4798
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...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.