Possible bugs in using [Flags] and Interfaces with Web Applications

H

Horia Tudosie

Using Visual Studio 2003

This is to report a series of bugs regarding the FlagsAttribute and
(independently) the usage of interfaces in Web applications.

Let’s declare xColors type like:

[FlagsAttribute]
public enum xColors {
Red = 1,
Yellow = 3,
Green = 2,
Blue = 4
}

A.
Let’s use it to create a WebComponent ColorLabel (see attachment). Let’s
play with it in the IDE (add a ColorLabel on an empty Web aspx page):

Enter the value 1 (or choose “Red” from the combo box) in the
corresponding property of the IDE. On the spot, the component displays
the text “Red”. Run the application – the same values appears in the
browser – OK.

Enter the value 6 (cannot choose “Green” and “Blue”!!! – kind of
mistake! Delphi can.) in the corresponding property of the IDE. On the
spot, the component display the text “Green, Blue” – kind of OK. Run the
application – an error comes: “Object reference not set to an instance
of an object.” The Stack Trace reveals an error in between the usage of
the System.Web.Compilation.CodeDomUtility.GenerateExpressionForValue and
System.Web.UI.TemplateParser.GetParserCacheItemThroughCompilation
methods. All test I have conducted points than an error occurs in
between interpreting the HTML code of the aspx source:

<cc1:ColorLabel id="ColorLabel1" style="Z-INDEX: 101; LEFT: 89px;
POSITION: absolute; TOP: 58px" runat="server" CompoziteColors="Green,
Blue">

and the call of the dll. The involved interpreter (whatever) correctly
translates the string “Green, Blue” in value 6, but 6 is not among the
enumeration considered without the attribute FlagsAttribute. To stress
this, replace in the HTML source the above string with “Red, Green” and
run the application – the browser displays “Yellow” – that means that
the implied interpreter correctly converted Red in 1 and Green in 2,
then correctly or-ed them giving 3. 3 went inside some sort of
validating and being among the enum values was translated in the
corresponding name – “Yellow” not “Red, Green”.

B.
A similar bug reveals itself at the same level when trying to use
interfaces.

The SameLike property was designed to force the component to copy the
behavior of another component when specified. In my big application I
have different components (Edit boxes, combo boxes, check boxes and
radio buttons and data grids) that could share the same Security
Manager. The property, which would drop down in its combo box all other
components candidates for this, can only have the type of the interface
of this manager (inner) object.

In this example I have introduced the interface IColor (see attachment
files.)

Add and select a/the second ColorLabel. Click the drop-down arrow of the
SameLike property. The combo box displays ColorLabel1 and ColorLabel2.
Select ColorLabel1.

On the spot, the second label displays the same value as the first one.

(Go to the first one and change its value – the second one does not
change, but this is understandable, since the page do not refresh; just
go to the second one and alter its Text property – whatever you enter,
it now reset to the value of the other label.)

Conclusion: the IDE correctly use the interface to identify other
components suitable for this property.

Now run the application – an error comes: “Unable to generate code for a
value of type 'ColorsComp.IColor'. This error occurred while trying to
generate the property value for SameLike.” And the Stack Trace reveals
the error somewhere in the same area of the previous bud – when a sort
of interpreter tries to convert from the string:

<cc1:ColorLabel id="ColorLabel4" style="Z-INDEX: 102; LEFT: 436px;
POSITION: absolute; TOP: 103px" runat="server" SameLike="ColorLabel3">

the value "ColorLabel3" to the IColor interface for placing it in the
RunAtServer dll.

C.
Other small bugs have been detected during this process:
Observe the declaration:

public override string Text {
set { base.Text = value; }
get { return base.Text; }
}

Basically, this could be omitted. However without this, and in spite of
the declaration
“RefreshProperties(System.ComponentModel.RefreshProperties.All),“ for
the CompoziteColor property, the Text property does not refresh inside
the IDE (but it does on the design page!) whatever attribute you may
substitute for “All”.

Horia Tudosie
Computer System Analyst
TravTech Inc.
www.pathcom.com/~horiatu
(e-mail address removed)

Sources:
----------- IColor.cs:

using System;

namespace ColorsComp
{
/// <summary>
/// Summary description for IColor.
/// </summary>
public interface IColor
{
xColors CompoziteColors {
get; set;
}
}
}

----------- ColorType.cs

using System;

namespace ColorsComp
{
/// <summary>
/// Summary description for ColorType.
/// </summary>
[FlagsAttribute]
public enum xColors {
Red = 1,
Green = 2,
Yellow = 3,
Blue = 4
}
}

----------- ColorLabel.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ColorsComp
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:ColorLabel runat=server></{0}:ColorLabel>")]
public class ColorLabel : System.Web.UI.WebControls.Label, IColor {

static private xColors lastDefault;

static ColorLabel() {
lastDefault = xColors.Blue;
}

public ColorLabel() {
switch (lastDefault) {
case xColors.Red : lastDefault = xColors.Green; break;
case xColors.Green : lastDefault = xColors.Blue; break;
case xColors.Blue : lastDefault = xColors.Red; break;
}
xcolor = lastDefault;
}

private xColors xcolor;

[Bindable(false), DefaultValue(xColors.Red),
RefreshProperties(System.ComponentModel.RefreshProperties.All),
Description("Combinable colors property.")]
public xColors CompoziteColors {
get { return (SameLike==null) ? xcolor:SameLike.CompoziteColors; }
set { xcolor = value; }
}

private IColor ic = null;

[Bindable(false), DefaultValue(null),
RefreshProperties(System.ComponentModel.RefreshProperties.All),
Description("Ignore internal values and copy the behaviour of
another component through the IColor interface.")]
public IColor SameLike {
get { return ic; }
set { ic = (((IColor)value).Equals((IColor)this)) ? null : value;
}
}

public override string Text {
set { base.Text = value; }
get { return base.Text; }
}

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output) {
base.Text = CompoziteColors.ToString();
base.Render(output);
}
}
}


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 

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