views:

214

answers:

2

Hi, I was trying to use tis depencency property in my code but it gives me error says that Default value type does not match type of property 'MyProperty'.But short should accept 0 as default value.Ans also if i try to give null as default value it works..even if its a non nullabel type.How come this happens..

public short MyProperty
        {
            get { return (short)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(short), typeof(Window2), new UIPropertyMetadata(0));
A: 

public short MyProperty { get { return (short)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(short), typeof(Window2), new UIPropertyMetadata((short)0));

This seems to work...looks like 0 will be interpreted as int..but why..?

biju
UIPropertyMetadata constructor gets object parameter, so there is no conversion. C# spec says that integer literals are used to write values of types int, uint, long, and ulong. When you write 0 without cast you get an int.
majocha
Don't answer your own question. Update it.
Will
@WillIf i update my question then it will no longer be a question..then how will it benifit other people with same probs...
biju
You are allowed to answer your own questions, see FAQ.
Wallstreet Programmer
@biju you didn't answer it, you updated your question. "looks like 0 will be interpreted as int, but why?" That implies you're updating your question with more information, not giving everybody an answer. Also, could you please edit this answer and fix your formatting? FFS its all screwed up...
Will
@Will ok i didnt answer my question.I posted a workaround..and i asked the question to keep the discussion open..ans as you can see,i got the answer i was looking for.If you cant answer it why dont you just look away...why bother...?
biju
@biju *FOR THE COMMUNITY!!!*
Will
@Will..The community is all fine..just don't overkill it..
biju
@biju okay, I was lying. Its just one of those annoying things noobs do. Like not being able to format their Q/A's properly. You still have your code all screwed up.
Will
@ Will..Thanks But the question is already answered..The newbee question with screwed code brought up the attention of some Geeks and guess what,I got what i was looking for..Thanks for your attention anyway..
biju
+1  A: 

The problem is that the C# compiler interprets literal values as integers. You can tell it to parse them as longs or ulongs (40L is a long, 40UL is ulong), but there isn't an easy way to declare a short. Simply casting the literal will work:

public short MyProperty
{
    get { return (short)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}

public static readonly DependencyProperty MyPropertyProperty  = DependencyProperty.Register(
    "MyProperty", 
    typeof(short),
    typeof(Window2),
    new UIPropertyMetadata((short)0));
Abe Heidebrecht