Which one is right?

S

shapper

Hello,

I am working on a custom control and I created a property and a "New"
method.

What is the right way to define the property in "New" method:
_Name = name
or
Me.Name = name

Here is my code:

' Name
Private _Name As String = String.Empty
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property ' Name

Public Sub New(ByVal name As String)

_Name = name

' OR

Me.Name = name

End Sub ' New

Thanks,
Miguel
 
B

Barrie Wilson

shapper said:
Hello,

I am working on a custom control and I created a property and a "New"
method.

What is the right way to define the property in "New" method:
_Name = name
or
Me.Name = name

I can't see any particular reason to "broker" the initialization of _Name
via your public property; probably doesn't make much performance difference
but _Name = name is the more customary method AFAIK; why should you care
about customary? a bit easier for others to read your code ...
 
S

Scott M.

There are 2 schools of thought on this.

One school says that you'd probably have validation code in the set section
of your property that checks the incoming value to see if it is acceptable
and therefore you should set your value via the property to ensure that only
acceptable data is used (and possibly avoid exceptions).

The other school says that if you are designing the class, then no one would
know better than you what the acceptable values are for the property and so
it is unlikely that you'd pass bad data to your own property during its
initialization. This camp says avoid the property and just set the value
directly into your private field (thus saving any processing of the
validation of the value).

I think both schools have a point, but I also believe it is more generally
accepted to go the second way I've described.

-Scott
 
S

shapper

There are 2 schools of thought on this.

One school says that you'd probably have validation code in the set section
of your property that checks the incoming value to see if it is acceptable
and therefore you should set your value via the property to ensure that only
acceptable data is used (and possibly avoid exceptions).

The other school says that if you are designing the class, then no one would
know better than you what the acceptable values are for the property and so
it is unlikely that you'd pass bad data to your own property during its
initialization. This camp says avoid the property and just set the value
directly into your private field (thus saving any processing of the
validation of the value).

I think both schools have a point, but I also believe it is more generally
accepted to go the second way I've described.

-Scott

I think I agree more with first school you mentioned.
Sure I know what data I should have in my properties but if I am
creating a control for others to use then the validation makes some
sense and then using Me.Prop = prop might be the right choice, right?

Thanks,
Miguel
 
S

Scott M.

I think I agree more with first school you mentioned.
Sure I know what data I should have in my properties but if I am
creating a control for others to use then the validation makes some
sense and then using Me.Prop = prop might be the right choice, right?

Why? So what if you are making the control for others? Think about it for
a second. Microsoft makes all kinds of code for others to use, like the
System.Windows.Forms.Button class for example. Now, when you make an
instance of a button, it gets several properties already initialized. Are
those properties set by Microsoft using the private fields or the public
properties? I don't know for sure, but as the user, I don't care either. I
just know that when I instantiate a button, it has several properties that
already have default values. How those values got there has nothing to do
with how me, the user of the button, is going to use it.

Remember, we're only talking about initializing property values in your
constructor here, not the continued used of them by others. For
initialization, I'd say go to the private field value directly (because you
know how your own class's should be initialized) and when others use your
class, they have the public properties as their access points into your
private fields.

This is what building "black boxes" is all about and goes to the heart of
good object oriented design.

-Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top