Binding with structs

  • Thread starter Thread starter vuillet.thomas
  • Start date Start date
V

vuillet.thomas

Hi everybody !

I have a small strange problem and despite many tries, I can't find a
solution.

First, I'm using VS2005 with .NET CF 2.0.

I have an object which contains a public struct member. I want to bind
a property of this struct to a textbox on a a Form. After the bind, the
struct property is correctly displayed in the control but if I modify
the value in the textbox, I can see in debug that a struct's field is
updated, but it's not the one which I have in my object ! It's not easy
to explain like that, so here is some piece of code to reproduce :

// Here is a simple class which contains a public structure
public class MetierTest
{
public StructNature _objnature;

public MetierTest()
{
_objnature = new StructNature();
_objnature.Detail = "initvalue";
}
}

// Here is the struct definition
public struct StructNature
{
private string _detail;

public string Detail
{
get { return _detail; }
set { _detail = value; }
}
}

// Code used to bind the textbox to the "Detail" property of the
structure embedded in a MetierTest
// instance called _test
MetierTest _test = new MetierTest();
txtNatureDetail.DataBindings.Add("Text", _test._objnature, "Detail");

--> Result :
- "initvalue" is displayed in the textbox
- when modifying the textbox content and validate it, "set" of "Detail"
property in StrucNature is executed and the new value is set
- BUT _test._objnature.Detail is still equals to "initvalue" !

If I use a class instead of StructNature, it works but I would prefer
to use structures beacuse of perfomance reasons (there will be no
methods in this class, only properties).

Thank you for reading and for your help !

Bye,

Thomas.
 
That's probably because your structure is boxed at some point and data is
changed in this boxed copy.

Since it's boxed anyway, there will be little or no performance advantages
of using structures instead of class.


--
Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 
Thanks for your answer but could you explain why "it's boxed anyway" ?
Which operation in my code cause the structure to be boxed ?

Regards,

Thomas
 
Every time value type is passed as object a copy is created on a heap and
reference to it passed instead.

That is called "boxing". For example:



using System;



namespace tests
{



public class Class1
{
public static void Main()
{



int i = 10;
Console.WriteLine("Before: i={0}", i);
foo(i);
Console.WriteLine("After: i={0}", i);
}




static void foo(object o) {
Console.WriteLine("In foo before: i={0}", o);
o = 55;
Console.WriteLine("In foo after: i={0}", o);
}
}
}



That would produce the following output:



Before: i=10
In foo before: i=10
In foo after: i=55
After: i=10



Data binding engine operates with objects since it does not know which type
you're binding to.

That means value types would be boxed at some point if you're using data
binding.




--
Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 
Back
Top