Christopher Bennage

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

Silverlight Challenge: Creating a DependencyProperty

The following code throws an exception. Can you spot the bug?

public class MyControl : Control
{
    public static DependencyProperty MyPropertyProperty = DependencyProperty.Register(
        "MyProperty",
        typeof (double),
        typeof (MyControl),
        new PropertyMetadata(0));

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

Posted 03-09-2010 3:36 PM by Christopher Bennage
Filed under: ,

[Advertisement]

Comments

Hüseyin Tüfekçilerli wrote re: Silverlight Challenge: Creating a DependencyProperty
on 03-09-2010 4:15 PM

Use

new PropertyMetadata(0d)

instead of

new PropertyMetadata(0)

?

Jason Young wrote re: Silverlight Challenge: Creating a DependencyProperty
on 03-09-2010 4:16 PM

I can see two issues, the dependency property declaration should be marked readonly and the value passed to the PropertyMetadata should be a double, i.e. "0.0".

Cadred (dotNET) wrote dotNet News
on 03-09-2010 4:23 PM

dotNet News

Hadi wrote re: Silverlight Challenge: Creating a DependencyProperty
on 03-10-2010 3:48 AM

the only change needed is "new PropertyMetadata(0.0)", readonly is not necessarily needed.

Christopher Bennage wrote re: Silverlight Challenge: Creating a DependencyProperty
on 03-10-2010 10:17 AM

Correct. The default needs to be a double. The exception throw is a little vague, and I stared at this for about 10 minutes before it occurred to me. :-P

Regarding the 'readonly', it doesn't cause an exception but it's a good idea. It was an accident that I omitted it in the example.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)

CodeBetter.Com