Learning C# properties from O'Relly book - sample code problem

M

Michael Hanson

In a simple time class, int hour

/*
public int Hour // This works
{
get { return hour; }
set { hour = value; }
}
*/

// Automatic property?
public int Hour { get; set; } // This doesn't set hour !


// Client code
Time t = new Time(now);

int theHour = t.Hour;
Console.WriteLine("\nRetrieved the hour: {0}\n", theHour);
theHour++;
Console.WriteLine("\nUpdated the hour: {0}\n", theHour);
 
P

Peter Duniho

Michael said:
[...]
// Automatic property?
public int Hour { get; set; } // This doesn't set hour !

Why would it? How would the compiler know that you want your member
field "hour" to be set by the property named "Hour"?

If you have explicit needs for your property implementation, then you
have to implement the property explicitly.

Pete
 
M

Michael Hanson

Peter Duniho said:
Michael said:
[...]
// Automatic property?
public int Hour { get; set; } // This doesn't set hour !

Why would it? How would the compiler know that you want your member field
"hour" to be set by the property named "Hour"?

If you have explicit needs for your property implementation, then you have
to implement the property explicitly.

Pete

OK thanks much
 
M

Mr. Arnold

Michael said:
In a simple time class, int hour

/*
public int Hour // This works
{
get { return hour; }
set { hour = value; }
}
*/

// Automatic property?
public int Hour { get; set; } // This doesn't set hour !


// Client code
Time t = new Time(now);

int theHour = t.Hour;
Console.WriteLine("\nRetrieved the hour: {0}\n", theHour);
theHour++;
Console.WriteLine("\nUpdated the hour: {0}\n", theHour);

That works in Interfaces for me the Auto Property, but it's not an Auto
Property. An Interface property get/set works like an Auto Property.

public Interface Look
{
string SeeWhat {get; set;}
}

Also something like Reshaper which I use will point out when you can use
an Auto Property and convert the standard property over to an Auto
Property. But it's conditional and Reshaper is not asking on every
property to an Auto Property


Public class Help
{
public string Name {get; set;}
}

I make a class like the one above all with auto properties all the time
when bringing Objects across the wire from a Web service to the client
like the Help class/object.

I think it may be in certain situations that you can use the Auto property.


Also from what I have seen Reshaper work, it looks at a class like Help
class and if the property is a standard property it will ask to auto
convert it.

So, I guess I am saying you have to find out under what conditions can
you use an auto property.
 
J

Jeff Johnson

In a simple time class, int hour

/*
public int Hour // This works
{
get { return hour; }
set { hour = value; }
}
*/

// Automatic property?
public int Hour { get; set; } // This doesn't set hour !

The whole point of an automatic property is to prevent you from ever seeing
or touching the backing variable. When you use automatic properties, you
must always reference them via the property itself.
 
H

Hillbilly

Just the setter needs a private _hour member as the getter is still a magic
member.

Michael Hanson said:
Peter Duniho said:
Michael said:
[...]
// Automatic property?
public int Hour { get; set; } // This doesn't set hour !

Why would it? How would the compiler know that you want your member
field "hour" to be set by the property named "Hour"?

If you have explicit needs for your property implementation, then you
have to implement the property explicitly.

Pete

OK thanks much
 
P

Peter Duniho

Hillbilly said:
Just the setter needs a private _hour member as the getter is still a
magic member.

The setter cannot be provided a private "hour" member without explicitly
implementing the getter. You can't have both the private "hour" member
_and_ "a magic member" (by which I assume you mean the hidden,
auto-generated backing field that is created for an auto-implemented
property).

Pete
 

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

Top