Webservice with optional parameters

S

Sam Shrefler

I'm working on creating a WebService / WebMethod to receive a record
in real time from another system. The record contains about 20
fields. 10 of which aren't required.

I was planning on just making a method with 20 parameters. But, I see
there is no way to make an "optional" c# parameter. I was just
wondering if anyone had an suggestions on how to implement this? The
parameters are all different primitive types (int, string,
datetime)....

thanks

Sam
 
J

Jon Skeet [C# MVP]

I'm working on creating a WebService / WebMethod to receive a record
in real time from another system. The record contains about 20
fields. 10 of which aren't required.

I was planning on just making a method with 20 parameters. But, I see
there is no way to make an "optional" c# parameter. I was just
wondering if anyone had an suggestions on how to implement this? The
parameters are all different primitive types (int, string,
datetime)....

Make it take a single parameter of a type which encapsulates the
record. For optional reference type fields, use null to avoid
specifying the value. For optional value type fields, either have a
separate flag or have a "magic" value which means "no real
value" (e.g. int.MinValue for integers).

Jon
 
S

Sam Shrefler

Is there any way to use Nullable<int> or Nullable<dateTime> in the in
the record?

Thanks

Sam
 
S

Sam Shrefler

I'm sorry,I guess I didn't explain myself very well there.

I have a webMethod called saveUser

It must receive user information. Some of the information needs to be
optional. Some is required (represented with a *):

string firstName*
string middleName
string lastName*
dateTime birthday*
dateTime anniversary
dateTime death
int userTypeID

I need my method able to accept all of them, yet, it can't force them
to send an anniversaryDate time...

Any suggestions on something like that?

Thanks

Sam
 
G

Guest

Well, if that's the way you must do it, then why not just leave the optional
parameters "blank" - either an empty string ( "") or null, whichever fits
your types, when the values are passed into your method?

For a DateTime, you can pass DateTime.MinValue (which is not a "null") and
just check for that.

So, you can check to see if optional parameters have a value, and decide
what to do.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
 
S

Sam Shrefler

I'd rather not use minValue since this webservice will be consumed by
many different platforms. Instead what i am trying to do is declare
my params as DateTime? and int?

That way, hopefully the user can just send an empty param <myDateTime/
and that will be received as null.

I'm creating and waiting for some tests to occur.

Does this seem like a viable solution?
 
J

Jon Skeet [C# MVP]

Sam Shrefler said:
I'd rather not use minValue since this webservice will be consumed by
many different platforms. Instead what i am trying to do is declare
my params as DateTime? and int?

Using int.MinValue is a *lot* more portable than using nullable types.
How would you expect Record to be represented on a platform which
doesn't have nullable types?

Compare that with using int.MinValue, which will always be the minimum
value of a 32-bit 2's complement integer.

Using DateTime.MinValue is a bit riskier - you'd have to have a well-
defined date/time which is representable on all target platforms, and
use that everywhere.
 

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