473,412 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Which one is right?

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
Nov 23 '07 #1
4 1001

"shapper" <md*****@gmail.comwrote in message
news:bc**********************************@a39g2000 pre.googlegroups.com...
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 ...

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

Nov 23 '07 #2
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
"shapper" <md*****@gmail.comwrote in message
news:bc**********************************@a39g2000 pre.googlegroups.com...
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

Nov 23 '07 #3
On Nov 23, 1:53 am, "Scott M." <s...@nospam.nospamwrote:
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

"shapper" <mdmo...@gmail.comwrote in message

news:bc**********************************@a39g2000 pre.googlegroups.com...
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
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
Nov 23 '07 #4
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

Nov 23 '07 #5

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

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
21
by: listaction | last post by:
Folks, I'm trying to implement a tabbed effect with CSS and HTML - no graphics. for some reason, i'm unable to control the width of the tabs. I've pasted the code below. Any help /...
31
by: Spiro Trikaliotis | last post by:
Hello, I have a question regarding subtracting a pointer from another one. Assume I have two pointers p1 and p2, which both point to a memory area obtained with malloc(). Assume p1 = p2 + some...
3
by: Phil Stanton | last post by:
I have a number of queries which use code for the output of 1 or more fields. For example Address:GetAddress(AddressID, True, 60) Address ID Points to an Address in a table - Address Line1, Line...
1
by: metaperl | last post by:
On reddit.com, many moons ago, I downloaded some code which generated a page using HTML tables from a picture of the page you wanted. However, I dont have any author information in the code and...
2
by: Bill Jackson | last post by:
For example, class A: def __init__(self,a): self.a = a def __eq__(self, other): return self.a == other.a class B: def __init__(self,b):
2
by: sree reddy | last post by:
..cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
9
by: antonyliu2002 | last post by:
By default, IIS is configured to timeout a session in 20 minutes, which can be changed through the IIS config window. I use InProc sessionState mode. I can also set the session timeout in...
5
by: kulabhishek | last post by:
Hello, I have developed one application, and added it to right click pop-up menu of the folder. Whenever user right-clicks on folder and selects my application from pop-up menu, I want the path...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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...
0
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...
0
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,...
0
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...

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.