1

I have the following class:

 public abstract class AuditableTable : TableServiceEntity, IAuditableTable  
    {

    protected AuditableTable()
        : base()
    {
        Status = "3";
    }

    protected AuditableTable(string pk, string rk)
        : base(pk, rk) 
    { 
    }
    public string Status { get; set; }

Is there a simple way that I can make the value of Status always default to "3" when a new instance is created? What is the way that most people use?

Update

I added a setter but then it will not set if the other constructor is called.

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

4 Answers4

3

You can do it in the constructor. That's the use of them.

public abstract class AuditableTable : TableServiceEntity, IAuditableTable  
{

    protected AuditableTable()
        : base()
    {
        InitFields();
    }

    protected AuditableTable(string pk, string rk)
        : base(pk, rk) 
    { 
        InitFields();
    }
    private InitFields()
    {
        Status = "3";
    }
    public string Status { get; set; }
}

UPDATE:

As you have updated the question, you can also look at this answer to rearrange your constructors.

Community
  • 1
  • 1
Maheep
  • 5,539
  • 3
  • 28
  • 47
  • Thanks. But now I wonder as I have more than one constructor. Is there some way I can make it so I don't have to code it in every constructor? – Samantha J T Star Dec 30 '11 at 04:45
  • @Samantha2 You need to decide on one constructor that does the assignment, and call that constructor from your other constructors using `: this()` syntax. – Sergey Kalinichenko Dec 30 '11 at 04:52
3
protected AuditableTable() { // no need to use : base(), it is called implicitly
    Status = 3;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

Do not use auto-implemented property. Declare a private field with initial value.

private int _status=3;

public int Status
{
  get { return _status;}
  set {_status=value;}
}

EDIT: Assign property value in constructor is good practice but if that class has more than one constructors than I think it will be good to initialize member at declaration.

Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Why do you need a field with backing property? You can simply use constructor. – Maheep Dec 30 '11 at 04:41
  • What happens if I try to set the value later? Will it still do 3 or do the new value. I can see the advantage of doing it this way as I have more than one constructor. What's the accepted practice? – Samantha J T Star Dec 30 '11 at 05:00
  • +1 Thank you for un-deleting this answer. In my opinion, it's the best way to handle things if you have multiple constructors that can't chain. – Jim Mischel Dec 30 '11 at 05:01
  • @Samantha2: If you set the property later, it will take on the new value, just like an auto-implemented property does. – Jim Mischel Dec 30 '11 at 05:02
1

This is typically done by chaining constructors. You have every constructor call "up" through a common constructor. For example:

public class MyThing : MyBaseThing
{
    public MyThing()
        : this(null, null)
    {
    }

    public MyThing(string pk, string rk)
        : base(pk, rk)
    {
        Status = "3";
    }

    public string Status { get; set; }
}

That works well if you can chain constructors. But sometimes you'll have constructors that can't be chained. If that's the case, then you make sure that each of your "leaf" constructors calls a private Init() method of some kind, that does common initialization. That's kind of a pain in the neck at times, when you have a readonly field, because readonly fields can only be modified in the constructor--not in a method that's called by a constructor.

A deleted answer proposed using a property with an explicit backing field, rather than using an auto-implemented property. That's a perfectly valid and quite reasonable thing to do, negative comments notwithstanding. Sure, it's not as pretty as an auto-implemented property, but it's very effective. It's what we did before we had auto-implemented properties, and there's no reason not to do it if you really need a default value for a property. In case you can't see the deleted answer:

public class MyThing : MyBaseThing
{
    private string _status = "3";
    public string Status
    {
        get { return _status; }
        set { _status = value; }
    }

    // constructors go here.
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351