locked
How to set default value for margin, color and font in constructor RRS feed

  • Question

  • Hi,

    I want to set default value for margin, color and font in constructor like,

    public void AddControlToPanel(string type, string content, Thickness margin = new Thickness(0), FontWeight weight = 
    FontWeights.normal, Brush color = new SolidColorBrush(Colors.Orange), double size = 12, EventHandler handler = null) { }

    However it gives compiling errors on
    Thickness margin = new Thickness(0), 
    FontWeight weight = FontWeights.normal,
    Brush color = new SolidColorBrush(Colors.Orange)

    How to set default value for above 3 parameters?

    Thanks,
    York

    Wednesday, May 2, 2012 12:50 AM

Answers

  • Hi,

       You can only assign default values which are compile time constants. That means, you can set integers strings etc and it also means you cannot assign any objects using new. 

    This will work:  color takes null

       public void AddControlToPanel( string type,FontWeight weight,  Thickness margin , Brush color=null ,  string content="hi", double size = 12, EventHandler handler = null)
     

    These will not work:

            public void AddControlToPanel( string type,FontWeight weight,  Thickness margin = new Thickness(0) , Brush color=null ,  string content="hi", double size = 12, EventHandler handler = null)
            public void AddControlToPanel( string type,FontWeight weight,  Thickness margin = null , Brush color=null ,  string content="hi", double size = 12, EventHandler handler = null)
           as Margin ,FontWeight wont take either null or any object.

    Hope this helps Laughing 

     

    Wednesday, May 2, 2012 4:37 AM
  • Your way of setting can not do that. I even try to set margin, color and font as null as default, but still get compiler error. Any thought?

    You can try the code below:

            
    public Thickness MyMargin { get; private set; }
            public void AddControlToPanel(Thickness? margin = null)
            {
                this.MyMargin = margin ?? new Thickness(0);
            } 

     

     Use Thickness? margin=null

     Hope helpful.

    Thursday, May 3, 2012 3:08 AM

All replies

  • Hi,

     Your code is throwing an exception for those values because they are not compile time constants. So you need to initialize them within the code only.

    P.S   If you are using a constructor, dont use void  

      public  AddControlToPanel(string type, string content, Thickness margin, FontWeight weight, Brush color, double size = 12, EventHandler handler = null)
            {
                weight = FontWeights.Normal;
                margin = new Thickness(0);
                color = new SolidColorBrush(Colors.Orange);
                //rest of your code can be written below

            }

    Hope it Helps Laughing

    Wednesday, May 2, 2012 2:17 AM
  • I know they can be initialized that way. I want to set default value because if they are not present in the function call, default values will be set. Eg, AddControlToPanel("", "") will set margin, color, font, size and eventhandler as default value, while AddControlToPanel("", "", margin) will set margin by input while set others as default.

    Your way of setting can not do that. I even try to set margin, color and font as null as default, but still get compiler error. Any thought?


    Wednesday, May 2, 2012 4:00 AM
  • Hello,

    you can try to define default values in control style template.

    Wednesday, May 2, 2012 4:13 AM
  • Can you explain more on how to do it?

    Wednesday, May 2, 2012 4:29 AM
  • Hi,

       You can only assign default values which are compile time constants. That means, you can set integers strings etc and it also means you cannot assign any objects using new. 

    This will work:  color takes null

       public void AddControlToPanel( string type,FontWeight weight,  Thickness margin , Brush color=null ,  string content="hi", double size = 12, EventHandler handler = null)
     

    These will not work:

            public void AddControlToPanel( string type,FontWeight weight,  Thickness margin = new Thickness(0) , Brush color=null ,  string content="hi", double size = 12, EventHandler handler = null)
            public void AddControlToPanel( string type,FontWeight weight,  Thickness margin = null , Brush color=null ,  string content="hi", double size = 12, EventHandler handler = null)
           as Margin ,FontWeight wont take either null or any object.

    Hope this helps Laughing 

     

    Wednesday, May 2, 2012 4:37 AM
  • Hi

    You can set default values for the control in style and you can refer that style to that control

    for eg. set a style for that control

    <Style x:Key="StackPanelStyle1" TargetType="StackPanel">
       <Setter Property="Width" Value="500"/>
       <Setter Property="Height" Value="500"/>
       <Setter Property="Background" Value="#FFEFA7A7"/>
       <Setter Property="Margin" Value="10"/>
      </Style>

    and use that style in that control for setting the default value like this

    <StackPanel Style="{StaticResource StackPanelStyle1}"/>

    the control will take the default values from the style...

    Wednesday, May 2, 2012 5:14 AM
  • Why Brush and EventHandler can take on null while FontWeight and Thickness can not?

    Wednesday, May 2, 2012 10:29 PM
  • Hi,

    We get the following messages when we assign null .   

    A value of type <null>cannot be used as a default parameter because there are no standard conversions to type System.Windows.Thickness   and  

    A value of type <null>cannot be used as a default parameter because there are no standard conversions to type System.Windows.FontWeight.  

    Hope it Helps Laughing

     

    Thursday, May 3, 2012 1:06 AM
  • Your way of setting can not do that. I even try to set margin, color and font as null as default, but still get compiler error. Any thought?

    You can try the code below:

            
    public Thickness MyMargin { get; private set; }
            public void AddControlToPanel(Thickness? margin = null)
            {
                this.MyMargin = margin ?? new Thickness(0);
            } 

     

     Use Thickness? margin=null

     Hope helpful.

    Thursday, May 3, 2012 3:08 AM