c# - Why does Silverlight View not throw a null exception? -
given silverlight view following binding:
<textbox width="200" text="{binding customer.firstname, mode=twoway}"/>
and code behind has following:
customerclass customer {get; set;}
this not throw nullreferenceexception
following
string firstname { { return customer.firstname; } }
does when attempt bind firstname
instead of customer.firstname
, why , how corrected? (other binding directly customer.firstname
or initializing customerclass
object)
edit: address possible duplicate issue. thought binding still tried reference when view first initialized, not case? if can see difference between getting reference , being bound @ view time
i thought binding still tried reference when view first initialized, not case?
the binding process designed handle null values , check initial target reference given , not attempt action if null. remember binding process of reflection off of named path/location , not actual extraction of value.
when binding discovers location trying reflect off of customer.firstname
null, stops right there.
but when given binding of firstname
, location reference binding quite valid. bingo! when ultimate operation after binding goes extract value getter
called throws exception because either customer
or firstname
null.
how corrected?
look adding binding either targetnullvalue, alternate binding use when null. or directly providing fallbackvalue use while binding null.
or design gui not rely on sub properties of object may null.
Comments
Post a Comment