Property

S

shapper

Hello,

I have a class property as follows:
public string UnitPrice { get; set; }

But when I need to implement some validation logic, according to some
documentation I have the following:
public double UnitPrice {
get {
return _unitPrice;
}
set {
if (value >= 0.00) {
_unitPrice = value;
}
_errors.Add("UnitPrice", value + " is not valid. The unit
price must be larger than 0.00.");
}
}
double _unitPrice;

My questions are:
- Why the _unitPrice? Does the property needs this extra variable?
- And do I need to add the return _unitPrice in get even if it doing
exactly the same as in:
public string UnitPrice { get; set; }

Thanks,
Miguel
 
P

Peter Morris

- Why the _unitPrice? Does the property needs this extra variable?

Yes, otherwise the property isn't storing its state anywhere.

- And do I need to add the return _unitPrice in get even if it doing
exactly the same as in:
public string UnitPrice { get; set; }

public string UnitPrice { get; set; } is just short cut code. The compiler
will create the "backing member" to hold the state for you. If you want to
add logic to the getter or setter then you will need to write the whole
property implementation along with the backing member.
 
J

Jeff Johnson

set {
if (value >= 0.00) {
_unitPrice = value;
}
_errors.Add("UnitPrice", value + " is not valid. The unit
price must be larger than 0.00.");
}

I hope that's not your real code, otherwise methinks you're lacking an else
{ }.
 
S

shapper

I hope that's not your real code, otherwise methinks you're lacking an else
{ }.

I think the moment _unitPrice=value then it exits. Not?
The code is copied exactly from release notes of the new version in
ASP.NET MVC RC1.

Another thing that is confusing me is double _unitPrice; being
declared After the property.
Shouldn't be before? This is strange to me ...

Thanks,
Miguel
 
I

Ignacio Machin ( .NET/ C# MVP )

I think the moment _unitPrice=value then it exits. Not?
The code is copied exactly from release notes of the new version in
ASP.NET MVC RC1.

you have to declare _unitPrice as a member variable:

class X{
double unitPrice;
Another thing that is confusing me is double _unitPrice; being
declared After the property.
Shouldn't be before? This is strange to me ...

The order in the file does not matter, you can call a method that is
defined below.
 
S

shapper

you have to declare _unitPrice as a member variable:

class X{
 double unitPrice;




The order in the file does not matter, you can call a method that is
defined below.

Yes, but as Jeff said shouldn't the errors part be inside an else like
that:

set {
if (value >= 0.00) {
_unitPrice = value;
} else {
_errors.Add("UnitPrice", value + " is not valid. The unit price
must be larger than 0.00."); }
}

instead of:

set {
if (value >= 0.00) {
_unitPrice = value;
}
_errors.Add("UnitPrice", value + " is not valid. The unit price
must be larger than 0.00.");
}

Thanks,
Miguel
 
J

Jeff Johnson

I hope that's not your real code, otherwise methinks you're lacking an
else
{ }.
I think the moment _unitPrice=value then it exits. Not?

Not. You must EXPLICITLY return from the setter; under the covers it's just
a regular function. The code above executes the _errors.Add() method whether
or not the if () condition passes.
 

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

Similar Threads

Class, Constructor and Property 9
Property. What am I missing? 2
Creating a Business Logic Layer 1
Override property 18
Intersection of Lists 5
Expand Range into List 2
Enum Extentions 7
IFormattable interface 3

Top