1

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.

1
  • Are you trying to reverse whatever is passed in value ? , it looks like you are trying to set value to false in true case of your if statement and true otherwise.? is that the case ? Commented Nov 19, 2015 at 19:21

1 Answer 1

7

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;}
Sign up to request clarification or add additional context in comments.

5 Comments

or just? -> public bool IsSecondHand {get;set;}
@JFM That won't set the private field
@MatiasCicero I think the suggestion is that a private backing field does not seem necessary here.
@crashmstr Can't really tell, OP may be showing us only a portion of the whole class
@Matias Cicero, well it's not exactly used either

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.