1

Below, when I attempt to use the _currentTemp variable, that was supposed to be auto-generated via the auto properties functionality, I get a variable not found message:

The name _currentTemp does not exist in the current context.

Using { get; set; } should automatically create this private variable (_currentTemp), right?

public class DogTemperature
{
    public double CurrentTemp { get; set; }

    public DogTemperature(double dCurrentTemp)
    {
        _currentTemp = dCurrentTemp;  //***this line***
    }
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • _"that was supposed to be auto-generated via the auto properties funcitonality"_ - can you explain what made you think so? – CodeCaster May 09 '16 at 15:20

3 Answers3

9

Backing fields created by auto-properties are not available for you to interact with in your source code, as it is generated by the compiler.

If you want to interact with the backing field, you'll need to create your properties the verbose way.

AntiTcb
  • 609
  • 4
  • 15
  • 1
    Another useful post: http://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value?rq=1 - might be useful – Geoff James May 09 '16 at 15:08
  • 2
    _"not available for you to interact with in your source code, as it is generated by the compiler"_ isn't really correct. The latter does not exclude the former. Using reflection, you can find and access the field. "not [directly] accessible" is a more correct statement. – CodeCaster May 09 '16 at 15:26
  • @CodeCaster: Reflection, my sworn enemy. – AntiTcb May 09 '16 at 15:59
  • 1
    There's nothing inherently wrong with reflection, you just have to know when to apply it. – CodeCaster May 09 '16 at 15:59
-2

Based on @Alex Gravely's answer...

If I'm understanding your necessity for a full property: you can create full properties and backing fields like this:

private double _currentTemp;

public double CurrentTemp
{
     get { return _currentTemp; }
     set { _currentTemp = value; }
}

Then in your constructor for DogTemperature, you just need to set the CurrentTemp to the double you passed in:

public void DogTemperature(double temp)
{
    DogTemperature = temp;
}

Depending on what usage you want to get out of the CurrentTemp property - i.e. to display in a View and updating it; you may want to read into implementing INotifyPropertyChanged. Here's a link: https://msdn.microsoft.com/en-us/library/ms229614(v=vs.100).aspx

If it is just a plain old property, and not used for anything special (such as in a model, for example); then the

public double DogTemperature { get; set; }

property will suffice; setting it in the constructor as above.

Hope this helps!

Geoff James
  • 3,122
  • 1
  • 17
  • 36
-2

In my opinion defining a property like this is complely pointless if all you want to do is store a value.

double _currentTemp;
public double CurrentTemp
{
   get { return _currentTemp; }
   set { _currentTemp = value; }
}

All you're doing here is giving the private context two ways to set the same value. You can set the _currentTemp field directly or you can set the CurrentTemp property which sets the _currentTemp field. If you are not doing anything with the property then just use the default get/set like this:

public double CurrentTemp { get; set; }

If you need to do more complex work in the property then go ahead and define a field like this. More complex work such as conditions, calculations or raising events:

double _currentTempFarenheit;
double _currentTempCelcius;

public double CurrentTemp
{
   get 
   { 
       if(UseFarenheit)
           return _currentTempFarenheit;
       else
           return _currentTempCelcius;
   }
   set 
   { 
       if(UseFarenheit)
           _currentTempFarenheit = value;
       else
            currentTempCelcius = value;
   }
}

Furthermore if you only want the value of your property to be set by the constructor of your DogTemperature class then you should make the setter private. This will only allow the property to be publically read.

public double CurrentTemp { get; private set; }
CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • You can use either the default `{ get; set; }`, or a more complex way of `get`/`set` calculating; but that depends on the usage of the property in the first place. The long-winded full property method is useful if you're needing to raise PropertyChanged events - not pointless at all – Geoff James May 09 '16 at 15:13
  • @GeoffJames Yes of course, using the PropertyChanged events falls into the "more complex" example i gave. If you are simply providing a value and nothing more then its pointless to use the long winded way. – CathalMF May 09 '16 at 15:15
  • @CathaIMF: On its own - yes; agreed. Again, depends on the usage of the property in the first place. For example: I tend to not have `INotifyPropertyChanged` implementation in just model classes and use only `{ get; set; }` declaration. More obviously; it makes sense to have these sorts of properties in ViewModels – Geoff James May 09 '16 at 15:19