Problem using enum as web method parameter

R

Ren

Hello all,

I am trying to localize some of the web methods in my web service so
I've created an enum in my web service of the allowed languages:

public enum Language
english
french
end enum

The first parameter in my new web methods is of the type "language".
On my client side if I pass either "english" or "french" everything is
fine, however if I leave the parameter blank I receive the error:

Cannot convert to WebService.Service+Language.
Parameter name: type --> Must specify valid information for parsing in
the string.

Is there anyway that I can accept either a blank (null) or an enum
value for this parameter without throwing this error if the value is
blank? I'd like to be able to default the language to english if no
language is specified.

thanks for you help

Ren
 
J

John Saunders [MVP]

Ren said:
Hello all,

I am trying to localize some of the web methods in my web service so
I've created an enum in my web service of the allowed languages:

public enum Language
english
french
end enum

The first parameter in my new web methods is of the type "language".
On my client side if I pass either "english" or "french" everything is
fine, however if I leave the parameter blank I receive the error:

Cannot convert to WebService.Service+Language.
Parameter name: type --> Must specify valid information for parsing in
the string.

Is there anyway that I can accept either a blank (null) or an enum
value for this parameter without throwing this error if the value is
blank? I'd like to be able to default the language to english if no
language is specified.

Could you do that in VB? Is blank or null a valid value for your enum?

If you're using .NET 2.0, you could try using a nullable type for your
parameter. It would be of type Nullable(Of Language).
 
R

Ren

Could you do that in VB? Is blank or null a valid value for your enum?

If you're using .NET 2.0, you could try using a nullable type for your
parameter. It would be of type Nullable(Of Language).


Hi John, thanks for your reply. I should have specified that I'm
still using version 1.1 of the framework and coding in VB. I'm not
sure how to specify blank as a valid value in my enum since "" is an
invalid value. I'd like to specify both "" (blank) and Nothing as
valid values in the enum but don't think that's possible.

I suppose that I could try and enclose the enum in a wrapper class.
I'm coming to the conclusion that an enum is probably not the best way
to do what I'm trying to do.
 
J

John Saunders [MVP]

Ren said:
Hi John, thanks for your reply. I should have specified that I'm
still using version 1.1 of the framework and coding in VB. I'm not
sure how to specify blank as a valid value in my enum since "" is an
invalid value. I'd like to specify both "" (blank) and Nothing as
valid values in the enum but don't think that's possible.

I suppose that I could try and enclose the enum in a wrapper class.
I'm coming to the conclusion that an enum is probably not the best way
to do what I'm trying to do.

My point was that, since you can't use blank or null as a value for an enum
in VB, it doesn't make sense to try to do that in a web service.
 
R

Ren

My point was that, since you can't use blank or null as a value for an enum
in VB, it doesn't make sense to try to do that in a web service.

Thanks for the clarification John. What would be the best way to
implement this then, if I want to have only the options "english,
french, blank/nothing" shown in the wsdl? Thanks for your patiance,
I'm still fairly knew to all this.
 
J

John Saunders [MVP]

Ren said:
Thanks for the clarification John. What would be the best way to
implement this then, if I want to have only the options "english,
french, blank/nothing" shown in the wsdl? Thanks for your patiance,
I'm still fairly knew to all this.

In the schema, define the element as a simple type, restriction of
xs:token, with enumerations of "english" and "french", and with a
minLength=0. If the element is also minOccurs=0, that should take care of
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

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top