Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / September 2004

Tip: Looking for answers? Try searching our database.

Why did .NET set a default attribute value?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott Liu - 23 Sep 2004 23:47 GMT
HI, All,

I have a web service doing a string search. It has an operator and a
searchValue field. The operator is defined as an attribute and required. The
xml is as below.

    <!-- SearchStringField -->
    <simpleType name="SearchStringFieldOperator">
        <restriction base="xsd:string">
            <enumeration value="is"/>
            <enumeration value="isNot"/>
        </restriction>
    </simpleType>
    <complexType name="SearchStringField">
        <sequence>
            <element name="searchValue" type="xsd:string"/>
        </sequence>
        <attribute name="operator" type="SearchStringFieldOperator" use="required"/>
    </complexType>

It works when I explicitly set the operator attribute since it is required.
One interesting problem I found is that when the operator is not set in my
.NET (c#) web service client the .NET studio sets a default value of “is” to
the operator. Since I do not have a default value chosen in my object
definition I do not see why .NET picks up one for me.

Included: c# references.cs

   /// <remarks/>
   [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:mynamespace")]
   public class SearchStringField {
       
       /// <remarks/>
       public string searchValue;
       
       /// <remarks/>
       [System.Xml.Serialization.XmlAttributeAttribute()]
       public SearchStringFieldOperator @operator;
   }
   
   /// <remarks/>
   [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:mynamespace")]
   public enum SearchStringFieldOperator {
       
       /// <remarks/>
       @is,
       
       /// <remarks/>
       isNot,
       }

Any help is appreciated greatly,

Thanks,

Scott
Derek Harmon - 27 Sep 2004 01:17 GMT
> One interesting problem I found is that when the operator is not set in my
> .NET (c#) web service client the .NET studio sets a default value of "is" to
[quoted text clipped - 7 lines]
>         isNot,
>         }

When an Enum is defined without explicit values, then .NET assigns
values starting with 0 and increasing.  So the above declaration is
actually equivalent to,

   public enum SearchStringFieldOperator {
       @is = 0,
       isNot = 1,
   }

Enums are represented as integers, and the field @operator you have
is of the SearchStringFieldOperator enum type, too.  Since enums are
really integers, this field is initialized to 0, which is the same integer
value as the first enum literal in the above declaration, @is.  This is
why when the Framework constructs a default SearchStringField the
@operator field already has the default value, @is.

If you want to avoid this behavior, then I'd try changing the definition
of the SearchStringFieldOperator enum.

   public enum SearchStringFieldOperator {
       @is = 1,
       isNot,
   }

This way, none of the literals of SearchStringFieldOperator correspond
to zero, which your enum fields will be initialized to by default.

Derek Harmon
Scott Liu - 28 Sep 2004 08:33 GMT
Thanks, Derek. This explains it.

The field definition is generated by .NET framework thus I do not want to
change it. The other solution then is to add a dummy field "notSet" as the 0
th enum value or to choose a default value explicitly in the schema.

Scott

> > One interesting problem I found is that when the operator is not set in my
> > .NET (c#) web service client the .NET studio sets a default value of "is" to
[quoted text clipped - 36 lines]
>
> Derek Harmon
Scott Liu - 28 Sep 2004 21:09 GMT
On second thought is there a preference in .NET which I can set to instruct
it to ignore providing a default value?

Thanks,

Scott

> Thanks, Derek. This explains it.
>
[quoted text clipped - 44 lines]
> >
> > Derek Harmon

Rate this thread:







Free Magazines

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

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.