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 / .NET Framework / XML / October 2005

Tip: Looking for answers? Try searching our database.

How to send a null value for a float variable?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robb Gilmore - 11 Oct 2005 17:20 GMT
Hello,

We have a C#.NET app which is calling a Java webservice.  We use the wsdl
file exportted from the java webservice to create our web-reference in Visual
Studio.  We are able to create the parameter classes and call the webservice
just fine.

Our problem is, within our .Net app, we have some value objects ( like
floats, for instance ) which are meant to be null.  Since there is no null
float, we use float.minvalue to indicate a null in this field.  When we call
the webservice, we don't want to pass float.minvalue for those fields, we
would like to translate it back into a true null and pass that.

Our first attempt was to edit our .wsdl file and replace nillable="true"
with minOccurs="0".   Then, within our .Net code, we do not set the float
value at all when we intend for it to null.  However this had the effect of
sending nulls for all of our float values.

Can you tell me the recommended way to send a null value in a float field,
if there is a recommended way?

thanks
Robb
"Jeffrey Tan[MSFT]" - 12 Oct 2005 09:10 GMT
Hi Robb,

Currently we are looking for someone to help you on this issue, we will
reply to you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Kevin Yu [MSFT] - 13 Oct 2005 07:27 GMT
Hi Robb,

As far as I know, there is no way to send a null for value types in .NET
1.1, because the value types cannot be null. In .NET 2.0 there will be
nullable types available. But I don't think java will support this. So,
currently, your resolution seems to be fine, and I don't have better ones
than yours.

Kevin Yu
Signature

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

Chris Lovett - 14 Oct 2005 18:34 GMT
A better solution might be to serialize this float as a string and do your
own mapping for null values internally as follows:

       private float price;
       private bool hasPrice;

       [XmlElement("Price")]  // this is the serializable property
       public string PriceString {
           get {
               if (!hasPrice) return null;
               return price.ToString();
           }
           set {
               this.Price = float.Parse(value);
           }
       }

       [XmlIgnore]        // this is not serialized, but it is what you use
in your program.
       public float Price {
           get { return this.price; }
           set { this.price = value; hasPrice  = true; }
       }

If you are using .NET 2.0 then you can simplify this further using the new
nullable type operator "?" as follows:

       private float? price;

       [XmlElement("Price")]  // this is the serializable property
       public string PriceString {
           get {
               if (!price.HasValue) return null;
               return price.Value.ToString();
           }
           set {
               this.Price = float.Parse(value);
           }
       }

       [XmlIgnore]        // this is not serialized, but it is what you use
in your program.
       public float Price {
           get { return this.price.HasValue ? this.price.Value : 0; }
           set { this.price = value;  }
       }

The XmlSerializer in .NET 2.0 also handles the nullable type, but it writes
out <Price xsi:nil="true" />.  If this is ok then you can simplify it even
further and write:

       public float? Price;

and be done!

> Hello,
>
[quoted text clipped - 23 lines]
> thanks
> Robb

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



©2009 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.