Resolved Accessing existing objects and their private fields

bondra

Well-known member
Joined
Oct 24, 2020
Messages
77
Programming Experience
Beginner
Hi!

I'm trying to access a private field for an existing object. As a student of C# I want to use best practises. But have stumbled upon some questions.

I want to access the bool value of playerIsComputer from the object Player.
I'm a bit confused however about the auto property vs non-auto.

Using the non-auto property let me access the field. However, will the field still be protected? Meaning I'm not actually messing with the backing field? Thus an okey solution according to best practise?

Using non-auto property:
class Player {
    // Backing fields
    private bool playerIsComputer;
    
    // Property
    public bool PlayerIsComputer {
        get { return playerIsComputer; }
        set { playerIsComputer = value; }
    }
}

Using the auto-property wont let me access the field at all:
Auto-property:
class Player {
    private bool PlayerIsComputer { get; set; }
}

And this is what I'm trying to achieve. To check wheater or not the player is a computer or not using an if-statement:
C#:
foreach (var player in players)
{
    if (player.PlayerIsComputer)
    {
        // Do something
    }
}
 
Solution
The getter of your property : get { return _playerIsComputer; } gets the field value : private bool _playerIsComputer;
The setter of your property : set { _playerIsComputer = value; } sets the field value : private bool _playerIsComputer;

This : public bool PlayerIsComputer { get; set; } does exactly the same thing as already explained on post p1/#5.
the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.
Change the private to public:
C#:
class Player
{
    public bool PlayerIsComputer { get; set; }
}

That is equivalent to what you originally had:
C#:
class Player
{
    private bool _playerIsComputer;
    public bool PlayerIsComputer
    {
        get { return _playerIsComputer; }
        set { _playerIsComputer = value; }
    }
}
 
Change the private to public:
C#:
class Player
{
    public bool PlayerIsComputer { get; set; }
}

That is equivalent to what you originally had:
C#:
class Player
{
    private bool _playerIsComputer;
    public bool PlayerIsComputer
    {
        get { return _playerIsComputer; }
        set { _playerIsComputer = value; }
    }
}

Thanks! Yes that I understand. But according to the instructions for the assignment all fields need to be private. So I'm a bit confused here on how to actually access them without setting them to public? Creating a new instance make me set the values. But I want to access existing objects and their fields.
 
The field is private and the property public. That is the same with auto-property that has a private hidden field as backing store.
the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.
 
I don't think you do understand. In the following code :

C#:
class Player
{
    private bool _playerIsComputer;
    public bool PlayerIsComputer
    {
        get { return _playerIsComputer; }
        set { _playerIsComputer = value; }
    }
}
This is a field : private bool _playerIsComputer;
And this is a property : public bool PlayerIsComputer

Read the link I linked and then see the link posted above in @JohnH's post.
 
I don't think you do understand. In the following code :

C#:
class Player
{
    private bool _playerIsComputer;
    public bool PlayerIsComputer
    {
        get { return _playerIsComputer; }
        set { _playerIsComputer = value; }
    }
}
This is a field : private bool _playerIsComputer;
And this is a property : public bool PlayerIsComputer

Read the link I linked and then see the link posted above in @JohnH's post.

Hehe well I think I get it now at least.
Just to clarify at its largest. Those two are then equal to each other. Just two different ways of writing it using non-auto vs auto:

Non-auto property:
C#:
class Player {
    private bool _playerIsComputer;
    
    public bool PlayerIsComputer{
        get {return _playerIsComputer;}
        set {_playerIsComputer = value;}
    }
}

Auto property:

C#:
class Player {
    public PlayerIsComputer {get; set;}
}
 
The getter of your property : get { return _playerIsComputer; } gets the field value : private bool _playerIsComputer;
The setter of your property : set { _playerIsComputer = value; } sets the field value : private bool _playerIsComputer;

This : public bool PlayerIsComputer { get; set; } does exactly the same thing as already explained on post p1/#5.
the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.
 
Solution
Back
Top Bottom