List Info

Thread: databinding to null objects




databinding to null objects
user name
2006-08-08 13:29:45
Well wasn't that the point of the question? It shouldn't
throw an
exception because null is a valid value to bind to

And besides, it's not true - I peered a little more at the
implementation of databinding and discovered the interesting
fact that
if you create the binding with formatting enabled, it works
just fine -
that code path allows you to bind to a null object so that
is a pretty
easy workaround (so I still claim it's a bug).

But this has made me realize that I don't really know
exactly what the
meaning of the formattingEnabled flag is. The documentation
is not very
helpful on this point. You can provide your own format
handler which
will be called irrespective of whether the binding is
created with
formatting enabled or not
  Andrew

-----Original Message-----
From: Discussion forum for developers using Windows Forms to
build apps
and controls [mailtoOTNET-WI
NFORMSDISCUSS.DEVELOP.COM] On Behalf Of
Vince P
Sent: Monday, August 07, 2006 6:50 PM
To: DOTNET-WINFORMSDISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-WINFORMS] databinding to null objects

Data Binding will always cause an exception if it encounters
a null
reference.

---
Vince
> -----Original Message-----
> From: Discussion forum for developers using Windows
Forms to build
apps
and
> controls [mailtoOTNET-WI
NFORMSDISCUSS.DEVELOP.COM] On Behalf Of
> Andrew Eames
> Sent: Monday, August 07, 2006 10:42
> To: DOTNET-WINFORMSDISCUSS.DEVELOP.COM
> Subject: [DOTNET-WINFORMS] databinding to null objects
>
> Hi All,
> I've run into an issue with databinding that I'm not
sure whether it
is a
bug or a
> feature - basically if I databind to a property of an
object where the
property is an
> object type and is null you get a FormatException
>
> The problem appears to be in the internal function
Binding.FormatObject
and it looks
> like a bug to me but I'm not a databinding expert so
I'm looking for a
second opinion
>
> You can repro this easily with the code below
>   Thanks for any input
>     Andrew
>
>   public class MyDataSource
>   {
>     public MyObject Foo
>     {
>       get { return foo_; }
>       set { foo_ = value; }
>     } MyObject foo_;
>   }
>
>   public class MyObject
>   {
>   }
>
>   public partial class UserControl1 : UserControl
>   {
>     public UserControl1()
>     {
>       InitializeComponent();
>     }
>
>     public MyObject SomeProperty
>     {
>       get { return someProperty_; }
>       set { someProperty_ = value; }
>     } MyObject someProperty_;
> }
>
>   public partial class Form1 : Form
>   {
>     public Form1()
>     {
>       InitializeComponent();
>       // next line throws FormatException
>      
userControl11.DataBindings.Add("SomeProperty",
dataSource_,
"Foo");
>     }
>
>     MyDataSource dataSource_ = new MyDataSource();
>   }
databinding to null objects
user name
2006-08-09 16:07:24
The "FormattingEnabled" flag basically means go
thro' the new improved
code path for databinding that we introduced in Whidbey. 

When we initially added the improved formatting logic in
Whidbey we
found some cases where we broke existing apps because of
some changes in
behavior and so we add this flag to only go thro' the new
formatting
logic when it was set to true so that existing apps
maintained the
existing behavior. 

The formatting handler is called in both cases because this
was a
pre-Whidbey feature. However the new formatting string is
only used when
FormattingEnabled is true. Hence the name.

Over the course of the Whidbey release we made several other
improvements in databinding that we only enable when this
flag is true
to maintain compatibility for existing applications. 

My recommendation would be to set this flag to true for new
apps

mark

-----Original Message-----
From: Discussion forum for developers using Windows Forms to
build apps
and controls [mailtoOTNET-WI
NFORMSDISCUSS.DEVELOP.COM] On Behalf Of
Eames, Andrew
Sent: Tuesday, August 08, 2006 6:30 AM
To: DOTNET-WINFORMSDISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-WINFORMS] databinding to null objects

Well wasn't that the point of the question? It shouldn't
throw an
exception because null is a valid value to bind to

And besides, it's not true - I peered a little more at the
implementation of databinding and discovered the interesting
fact that
if you create the binding with formatting enabled, it works
just fine -
that code path allows you to bind to a null object so that
is a pretty
easy workaround (so I still claim it's a bug).

But this has made me realize that I don't really know
exactly what the
meaning of the formattingEnabled flag is. The documentation
is not very
helpful on this point. You can provide your own format
handler which
will be called irrespective of whether the binding is
created with
formatting enabled or not
  Andrew

-----Original Message-----
From: Discussion forum for developers using Windows Forms to
build apps
and controls [mailtoOTNET-WI
NFORMSDISCUSS.DEVELOP.COM] On Behalf Of
Vince P
Sent: Monday, August 07, 2006 6:50 PM
To: DOTNET-WINFORMSDISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-WINFORMS] databinding to null objects

Data Binding will always cause an exception if it encounters
a null
reference.

---
Vince
> -----Original Message-----
> From: Discussion forum for developers using Windows
Forms to build
apps
and
> controls [mailtoOTNET-WI
NFORMSDISCUSS.DEVELOP.COM] On Behalf Of
> Andrew Eames
> Sent: Monday, August 07, 2006 10:42
> To: DOTNET-WINFORMSDISCUSS.DEVELOP.COM
> Subject: [DOTNET-WINFORMS] databinding to null objects
>
> Hi All,
> I've run into an issue with databinding that I'm not
sure whether it
is a
bug or a
> feature - basically if I databind to a property of an
object where the
property is an
> object type and is null you get a FormatException
>
> The problem appears to be in the internal function
Binding.FormatObject
and it looks
> like a bug to me but I'm not a databinding expert so
I'm looking for a
second opinion
>
> You can repro this easily with the code below
>   Thanks for any input
>     Andrew
>
>   public class MyDataSource
>   {
>     public MyObject Foo
>     {
>       get { return foo_; }
>       set { foo_ = value; }
>     } MyObject foo_;
>   }
>
>   public class MyObject
>   {
>   }
>
>   public partial class UserControl1 : UserControl
>   {
>     public UserControl1()
>     {
>       InitializeComponent();
>     }
>
>     public MyObject SomeProperty
>     {
>       get { return someProperty_; }
>       set { someProperty_ = value; }
>     } MyObject someProperty_;
> }
>
>   public partial class Form1 : Form
>   {
>     public Form1()
>     {
>       InitializeComponent();
>       // next line throws FormatException
>      
userControl11.DataBindings.Add("SomeProperty",
dataSource_,
"Foo");
>     }
>
>     MyDataSource dataSource_ = new MyDataSource();
>   }
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )