2

so for a user control, I have

public int Count { get; set; }

so that I can use this.Count in another method. The trouble is, I want to set a default for Count to something like 15. How do I set defaults?

razlebe
  • 6,916
  • 6
  • 38
  • 53
tester
  • 20,193
  • 21
  • 80
  • 120
  • Can't you just set "Count" to your default value upon initialization? – Daniel Mann Jun 03 '11 at 23:07
  • possible duplicate of [How do you give a C# Auto-Property a default value?](http://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value) – nawfal Jul 17 '14 at 21:19

7 Answers7

7

In the constructor of the userControl

public YourUserControl(){
   Count = 15;
}
manji
  • 45,615
  • 4
  • 87
  • 100
4

You will need to set it in the constructor of your class.

For example:

public partial class YourControl : UserControl
{
    public int Count { get; set; }

    public YourControl()
    {
         InitializeComponent();

         // Set your default
         this.Count = 15;
    }
}

Alternatively, if you use a backing field, you can set it inline on the field:

public partial class YourControl : UserControl
{
    private int count = 15;
    public int Count 
    {
        get { return this.count; } 
        set { this.count = value; } 
    }
Reed Copsey
  • 522,342
  • 70
  • 1,092
  • 1,340
1

If you want the object that creates your control to set the default, then they can use a property initializer when they new the control. If you want to set a default for the control, then you would set those defaults in the constructor.

Erik Funkenbusch
  • 90,480
  • 27
  • 178
  • 274
1

You set the default values for automatic properties in the constructor of the class.

public MyClass()
{
    Count = 15;
}
Colin Mackay
  • 17,309
  • 4
  • 61
  • 83
1

Or alternatively to setting the value in the constructor, which will likely work just fine here, use the long approach.

int _count = 15;
public int Count {
  get { return _count; }
  set { _count = value; }
}

Sometimes this way is useful if the appropriate constructor is not called for some reason (such as some Serialization/Activation techniques).

Happy coding.

1

Instead of using an auto implemented property you can write your own property based on an underlying field containing your default value:

private int _count = 15;
public int Count
{
    get { return _count; }
    set { _count = value; }
}
Marius Schulz
  • 14,538
  • 12
  • 58
  • 94
1

You can use the DefaultValueAttribute. More info here: http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx

This question is a possible duplicate: How do you give a C# Auto-Property a default value?

Community
  • 1
  • 1
Hasanain
  • 905
  • 8
  • 16
  • 1
    This doesn't set the default value :( I thought it did at one time and found out the hard way I was incorrect. Rather, it is used to allow serialization techniques (and such) to *not serialize* the default value and thus save space (e.g. `[DataMember(EmitDefault=false)]`). It is also used in designers for the "reset" value, etc. –  Jun 03 '11 at 23:11
  • @pst: You are right, I just saw the fine print... This is also helpful for the Visual Designer in Visual Studio. – Hasanain Jun 03 '11 at 23:23