469,331 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

How to send a null value for a float variable?

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
Nov 12 '05 #1
3 5875
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
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 12 '05 #2
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
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #3
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!


"Robb Gilmore" <rg******@newsgroup.nospam> wrote in message
news:F8**********************************@microsof t.com...
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

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

102 posts views Thread by junky_fellow | last post: by
29 posts views Thread by Jason Curl | last post: by
7 posts views Thread by dotnetnoob | last post: by
15 posts views Thread by khan | last post: by
14 posts views Thread by dopiotr | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.