ShouldSerialize question

C

Carlo

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
 
K

Kalpesh

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
 
C

Carlo

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
 
K

Kalpesh

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
 
C

Carlo

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
 
O

Oliver Sturm

Carlo said:
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
 
C

Carlo

Hi Oliver
today I read here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/custcodegen.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.
 
O

Oliver Sturm

Carlo said:
today I read here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/custcodegen.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
 
C

Carlo

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.
 
F

Frank Hileman

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

Carlo said:
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.
 
C

Carlo

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)
(e-mail address removed)


Frank Hileman said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top