Koosshh56
Koosshh56

Reputation: 317

C# bool object property validation

I have a class Shoes with a bool property to check if shoes are secondhand or not.

class Shoes
{        
    private bool secondhand;

    public bool IsSecondHand
    {
        get { return secondhand; }
        set
        {
            if (value == ) //don't know what to use here
            {
                value = false;
            }
            else
            {
               //is true
            }
        }
    }
}

My intention is to use this class (separate file) with a WPF window and using a checkbox that when checked makes my bool true, otherwise is false. I need to preserve the format get {} set {}. The problem is that this proprety should "work" even without the WPF part.

Upvotes: 1

Views: 288

Answers (1)

Glorin Oakenfoot
Glorin Oakenfoot

Reputation: 2623

Just simply do:

set
{
   secondhand = value;
}

Or, as @JFM suggests, you can simply use an auto property and you no longer need to explicitly declare the backing field:

public bool IsSecondHand {get; set;}

Upvotes: 7

Related Questions