.NET Forum / Windows Forms / Design Time / October 2005
ShouldSerialize question
|
|
Thread rating:  |
Carlo - 18 Oct 2005 12:10 GMT Hi all,
In my Form1, I have this statement:
Me.EnhancedPanelGreen2.Background.Color = System.Drawing.Color.GreenYellow
This is normal, because I've set to GreenYellow the Color property of the class Background in the component EnhancedPanelGreen2. Now, the following is the part of code of the class Background inside EnhancedPanelGreen2 component that controls the serialization:
Public Property Color() As Color Get Return mColor End Get Set(ByVal value As Color) mColor = value End Set End Property ' <Serialization> Protected Sub ResetColor() Color = Color.Green End Sub Protected Function ShouldSerializeColor() As Boolean Return Not Color.Equals(Color.Green) End Function
The problem is that changing Color to Green (that is the default of Color property) in the property browser of Form1, the statement inside my Form1 IS STILL:
Me.EnhancedPanelGreen2.Background.Color = System.Drawing.Color.GreenYellow
What I need (and what I expected) is that the above statement will disappears because the color (Green) is equal to the default(Green). Could someone try to explain me what I'm missing?
Thank you very much
Carlo
Kalpesh - 18 Oct 2005 12:18 GMT Hi Carlo,
I dont know - how does it relate to ShouldSerialize ? Moreover, if you change any of the property of any control, the VS.NET ide will generate code & put it in some specific location. e.g. "InitializeComponent"
So, if you have hand-coded anything specific, it will not be replaced by what you set from the properties window
Is that the case here ?
Kalpesh
Carlo - 18 Oct 2005 12:27 GMT Hi Kalpesh this is not the case. I've changed Color Property to Green from within the Property Window of the IDE. I simply expected that the statement with the new color (Green) will disappears. I've studied the problem from all the sides, finding no solutions
------------------------------------------- Carlo, MCP (Windows Based Applications) carlodevREMOVE@gmail.com
> Hi Carlo, > [quoted text clipped - 9 lines] > > Kalpesh Kalpesh - 18 Oct 2005 14:57 GMT Hi Carlo,
Are you able to change the color using the property window ? Before running the app, put a watch on whenever Color property changes. This will help you identify - what is it that is resetting the color & not taking your value at all
HTH Kalpesh
Carlo - 18 Oct 2005 15:22 GMT Hi Kalpesh the only problem is that the line
Me.EnhancedPanelGreen2.Background.Color = System.Drawing.Color.GreenYellow
is NOT removed by the designer when the color is set to Green.
Using the debugger not helped me to discover why. If you have two or three minutes of patience, I can send you a very small project that illustrates the problem. Carlo
------------------------------------------- Carlo, MCP (Windows Based Applications) carlodevREMOVE@gmail.com
> Hi Carlo, > [quoted text clipped - 5 lines] > HTH > Kalpesh Oliver Sturm - 18 Oct 2005 18:59 GMT >What I need (and what I expected) is that the above statement will >disappears because the color (Green) is equal to the default(Green). Could >someone try to explain me what I'm missing? I think - but I haven't tried it now - that the methods ResetXXX and ShouldSerializeXXX have to be public, not protected.
Oliver Sturm
 Signature Expert programming and consulting services available See http://www.sturmnet.org (try /blog as well)
Carlo - 18 Oct 2005 19:32 GMT Hi Oliver today I read here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/c ustcodegen.asp
that the better practice is to set Resetxxx and ShouldSerializexxx to private. However, I've tried to set Public, Private and Protected, without differences.
Anyway, I've just tried THE SAME source on VS2005beta2 and IT WORKS correctly! When the color is Green, the statement in Form1 is correctly deleted.
Now, I must find a valid workaround for VS2003.
 Signature ------------------------------------------- Carlo, MCP (Windows Based Applications) carlodevREMOVE@gmail.com
>>What I need (and what I expected) is that the above statement will >>disappears because the color (Green) is equal to the default(Green). Could [quoted text clipped - 4 lines] > > Oliver Sturm Oliver Sturm - 18 Oct 2005 19:51 GMT >today I read here > >http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/c ustcodegen.asp > >that the better practice is to set Resetxxx and ShouldSerializexxx to >private. Yes, I guess that's right as a general practice. I don't actually use these on a regular basis and I seemed to remember seeing many examples where these methods were public.
>Anyway, I've just tried THE SAME source on VS2005beta2 and IT WORKS >correctly! When the color is Green, the statement in Form1 is correctly >deleted. > >Now, I must find a valid workaround for VS2003. That's interesting. I'm quite sure I've done the same thing in VS 2003, so there must be something special about the exact code you're using that triggers this issue. Maybe you should try the obvious things first, like changing the property's name or type to see when the problem goes away.
Oliver Sturm
 Signature Expert programming and consulting services available See http://www.sturmnet.org (try /blog as well)
Carlo - 18 Oct 2005 20:18 GMT Hi Oliver yes, I tried all the obvious things first: changing property names, using variables instead of fixed values (i.e. Color.Green), changing scope of Raset/ShouldSerialize, and so on. In other words, I started to think to a bug when I noted the same unexpected behaviour with any type, not only colors. In fact, the problem I described in my post occours either with other types. In my sample program, I'm using a Color type and a Boolean, and the problem is the same. I confirm that this afternoon I installed VS2005/beta2 and the line
Me.EnhancedPanelGreen2.Background.Color = System.Drawing.Color.GreenYellow
is removed after changing the color from GreenYellow to Green.
I'm developing a collection of GDI+ objects (components), and I use the same base classes (i.e. Background class) for several components, and I need have different defaults for backgrounds of each component. Unfortunately, changing default of the base class (Background) from within a component is reverted to Background class default after serialization.
------------------------------------------- Carlo, MCP (Windows Based Applications) carlodevREMOVE@gmail.com
>>today I read here >> [quoted text clipped - 19 lines] > > Oliver Sturm Frank Hileman - 19 Oct 2005 15:12 GMT Hello Carlo,
You may not like this, but the solution in .net 1.1 is to write your components in C#. I have seen the same bug when using Visual Basic as well. It is confined to the use of value types. I was unable to find a work-around either.
We try to use C# as much as possible, but if a customer demands a component in VB, they simply have to live with the extra line of generated code, and the bolded text in the PropertyGrid.
C# has other advantages for component libraries, such as the xml code documentation that provides excellent intellisense and ndoc input.
Only the component author has to use C#; the component user can use VB.
Regards, Frank Hileman
check out VG.net: http://www.vgdotnet.com Animated vector graphics system Integrated Visual Studio .NET graphics editor
> Hi Oliver > yes, I tried all the obvious things first: changing property names, using [quoted text clipped - 44 lines] >> >> Oliver Sturm Carlo - 19 Oct 2005 15:49 GMT Hello Frank today I've sent to Oliver Sturm (see this ng thread) a small program that demonstrates my problem with serialization. He promptly reported me that the problem does not occurred on his system (even if I'm absolutely sure that the problem exists). So, I started to think that the problem is caused by the localized version of VS (VB designer) that I'm currently using (Italian). So, at the moment I'm planning to test an English version of IDE today.
Now you say that you had the same problem with VB, and now I got confused!
About your suggestion: I know the advantages of C#. Unfortunately, I'm not enought familiar with C# for programing a high-quality components collection as I'm doing. I'm studying C#, and I plan to switch to C# in the next half-year.
If the English version of the IDE will not solve my problem, I will adopt the drastic workaround of not using defaults for some values, accepting bold text in property browser.
Thank you for your help.
Carlo
------------------------------------------- Carlo, MCP (Windows Based Applications) carlodevREMOVE@gmail.com
> Hello Carlo, > [quoted text clipped - 70 lines] >>> >>> Oliver Sturm
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|