By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,069 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,069 IT Pros & Developers. It's quick & easy.

Best Practices - Coding Convention Question

P: n/a
Ren
Hi All,

I'm still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks
Mar 22 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ren wrote:
Hi All,

I'm still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks


Not sure of best practice on this, but from a practice level, if you use
the property it allows you to make changes during the get/set in the
future w/o having to recode rest of the class.

Chris
Mar 22 '06 #2

P: n/a
Ren,

I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.

By the way there is never a Best Practice. Luckily this is always a part of
discussion.

Typical for the VBNet developper is that he/she is human and can change when
it is needed and take the best Practice for that situation.

Cor

"Ren" <no****@nospam.org> schreef in bericht
news:pf********************************@4ax.com...
Hi All,

I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks

Mar 22 '06 #3

P: n/a
"Ren" <no****@nospam.org> schrieb:
I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?


I consider accessing it through the property's 'Get'/'Set' accessor even
inside the class whereever possible good practice, because it will prevent
assignment of invalid values to the property. Execution overhead is minimal
because the JIT compiler can inline property access.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 22 '06 #4

P: n/a
I agree with Cor and Herfried. There is no reason to treat the property
differently within its class. It can only be cause of errors. If you
feel you need it, it probably means you are doing something you should
not, or you are doing it the wrong way.

On the practical side the greatest advantage is that you just write
"me." and you have all the properties there readily available
(intellisense). It's a lot more convenient...

-tom

Mar 22 '06 #5

P: n/a
Cor ,
I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.

funny the parctical guidelines book states that m_VariableName should be
used :-)
By the way there is never a Best Practice. Luckily this is always a part
of discussion.

Typical for the VBNet developper is that he/she is human and can change
when it is needed and take the best Practice for that situation.
Have you ever heard of programming by MSF ?? MSF holds a colection of
practical guidelines and best practices wich includes naming conventions
So these rules do exist ............. however do we want to implement them
as strict as they are,, that is more the question

If we would code by these rules our programs would be more readable and
understandable , however practice learns that most coders prefer there own
conventions ( you still see hungarion notation in .net code for
stance ) or even worse program without anny conventions at all ( 90 % of
the code i saw in my daily work was like this, and i have seen code from
all over the world by all sorts of programmers ) ...

So the guidelines do exist MS has verry nice documentation regarding this ,
however we , as programming comunity do not enforce them or expect them to
be the standard ... that is the problem in my opinion .

see for more info about MSF :

http://www.microsoft.com/technet/its...f/default.mspx

personal recomendation regarding this :

Practical Guidelines and best practices for Microsoft Visual Basic and
visual C# Developers
http://www.amazon.com/gp/product/073...Fencoding=UTF8

Having said this i will start packing my suitcase as my plain leaves
tomorrow to the canary islands :-)
i will have a 2 week holliday before i start my new job

regards

Michel Posseth [MCP]

"Cor Ligthert [MVP]" <no************@planet.nl> schreef in bericht
news:eK**************@TK2MSFTNGP11.phx.gbl... Ren,

I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.

By the way there is never a Best Practice. Luckily this is always a part
of discussion.

Typical for the VBNet developper is that he/she is human and can change
when it is needed and take the best Practice for that situation.

Cor

"Ren" <no****@nospam.org> schreef in bericht
news:pf********************************@4ax.com...
Hi All,

I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks


Mar 22 '06 #6

P: n/a
Lee
Something not addressed in the other posts...

If you always reference the private variable by its property-name and
you need to search for where the property is referenced you only have
to search for one thing -- the property. Otherwise, within the class
wherein it's defined, you have to search for both the property-name and
the variable-name.

--
Lee Silver
Information Concepts Inc.

Mar 23 '06 #7

P: n/a
Ren wrote:
<snip>
I'm still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer <snip> end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class


Properties were created to protect, and even hide, the internal
structure of the object from the outside world. Ideally, only the
object itself should know about it's internal fields and how they
relate to the exposed properties.

Besides, properties allow for side-effects to happen when a given field
is accessed. Therefore, a property actually encapsulates a behavior.

Now, it's a very common practice to expose internal fields -- that were
suposed to be public -- through properties, even though these
properties are only getters/setters (I'm talking about just those
properties that are both a getter and a setter for a given field). The
reasoning is: a) you must not expose your internal fields and b)
properties protect you by keeping your interface intact should the
internal workings of the object change.

But It turns out that I see more frequent changes to an in-progress
object interface than to the internals of the object, usually because
of unantecipated needs of the "external world" (from the object's
perspective), even though the internal structure of the object remains
more or less intact.

Considering this, and as a left over from the time when compilers
wouldn't be smart enough to inline the access to properties, my
personal practice goes against (although not religiously) accessing
internal fields via getter/setter properties.

I mean, the object is in a priviledged position to see these fields, so
it should also exercise direct access to them.

Therefore, internally I'd use mVariableName, instead of the related
properties.

Regards,

Branco.

Mar 23 '06 #8

P: n/a
Michel,

Did you ever look at modern Visual Basic samples on MSDN (not from C++ -> C#
converted ones).

Microsoft has showed much of those patterns, it seems for me that it depends
who made it what it look like.

Cor

"Michel Posseth [MCP]" <MS**@posseth.com> schreef in bericht
news:ex**************@TK2MSFTNGP11.phx.gbl...
Cor ,
I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.


funny the parctical guidelines book states that m_VariableName should be
used :-)
By the way there is never a Best Practice. Luckily this is always a part
of discussion.

Typical for the VBNet developper is that he/she is human and can change
when it is needed and take the best Practice for that situation.


Have you ever heard of programming by MSF ?? MSF holds a colection of
practical guidelines and best practices wich includes naming conventions
So these rules do exist ............. however do we want to implement them
as strict as they are,, that is more the question

If we would code by these rules our programs would be more readable and
understandable , however practice learns that most coders prefer there own
conventions ( you still see hungarion notation in .net code for
ance ) or even worse program without anny conventions at all ( 90 % of
the code i saw in my daily work was like this, and i have seen code from
all over the world by all sorts of programmers ) ...

So the guidelines do exist MS has verry nice documentation regarding this
, however we , as programming comunity do not enforce them or expect them
to be the standard ... that is the problem in my opinion .

see for more info about MSF :

http://www.microsoft.com/technet/its...f/default.mspx

personal recomendation regarding this :

Practical Guidelines and best practices for Microsoft Visual Basic and
visual C# Developers

http://www.amazon.com/gp/product/073...Fencoding=UTF8

Having said this i will start packing my suitcase as my plain leaves
tomorrow to the canary islands :-)
i will have a 2 week holliday before i start my new job

regards

Michel Posseth [MCP]

"Cor Ligthert [MVP]" <no************@planet.nl> schreef in bericht
news:eK**************@TK2MSFTNGP11.phx.gbl...
Ren,

I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.

By the way there is never a Best Practice. Luckily this is always a part
of discussion.

Typical for the VBNet developper is that he/she is human and can change
when it is needed and take the best Practice for that situation.

Cor

"Ren" <no****@nospam.org> schreef in bericht
news:pf********************************@4ax.com...
Hi All,

I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do
I use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks



Mar 23 '06 #9

P: n/a
Hi Ren,

It depends on the case. In your example there is no difference, but there
are cases where the Get/Set accessors execute additional code (validation,
caching, etc) and in those cases you need to consider if you want that
additional code to be executed or not.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Ren" <no****@nospam.org> escribió en el mensaje
news:pf********************************@4ax.com...
Hi All,

I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks

Mar 23 '06 #10

P: n/a
"Branco Medeiros" <br*************@gmail.com> schrieb:
Besides, properties allow for side-effects to happen when a given field
is accessed. Therefore, a property actually encapsulates a behavior.

Now, it's a very common practice to expose internal fields -- that were
suposed to be public -- through properties, even though these
properties are only getters/setters (I'm talking about just those
properties that are both a getter and a setter for a given field). The
reasoning is: a) you must not expose your internal fields and b)
properties protect you by keeping your interface intact should the
internal workings of the object change.

But It turns out that I see more frequent changes to an in-progress
object interface than to the internals of the object, usually because
of unantecipated needs of the "external world" (from the object's
perspective), even though the internal structure of the object remains
more or less intact.

Considering this, and as a left over from the time when compilers
wouldn't be smart enough to inline the access to properties, my
personal practice goes against (although not religiously) accessing
internal fields via getter/setter properties.

I mean, the object is in a priviledged position to see these fields, so
it should also exercise direct access to them.


Consider this sample:

\\\
Private m_Bla As Integer

Public Property Bla() As Integer
Get
Return m_Bla
End Get
Set(...)
If Value < 0 OrElse Value > 10 Then
Throw New ArgumentException(...)
Else
m_Bla = Value
End If
End Set
End Property
///

When assigning the value to 'm_Bla' directly you are bypassing validation
and it's possible that the property value returned by 'Get' is an invalid
value due to bugs in the code which cause an invalid value to be assigned to
the private backing field. I strongly believe that it's better to have only
a single code path to access 'm_Bla' than multiple paths.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 23 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.