views:

49

answers:

2

Hi All,

I want to expose a web method over web services like this,

public bool AddApple(string colour);

The trouble with strings is that you can pass a value of null.

Is there a way to say that that string is non nullable?

For instance in the entity framework the property has an attribute like this,

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]

Which says the string is not nullable.

Just checking, because obviously if there is no way I will just have to perform validation on it and throw an error. The only problem is that my 'parameter' is actually a very complicated object with a number of strings in it, so it will take me a while to write the code to throw errors, and I could easily miss stuff.

Thanks.

+1  A: 

No. I don't think your can force string to be not null. You'll need to validate here.

Sidharth Panwar
OK. No harm in trying eh!
peter
@peter Hmm. I'd love to know if there's a workaround for this problem. Would be very useful.
Sidharth Panwar
I am writing a web service that is being exposed to a third party software development team. They are in a different country and where I am coming from is that I want to lock things down so that they can only do things in the correct way. Obviously I will never get there, but other things I have done will make things a bit less painful. E.g. using enums instead of assuming they will pass in particular strings.
peter
Hmm. Enums are always better if you've a limited set of string. Efficient too :)
Sidharth Panwar
A: 

Could you pass in Enums instead for your value of color instead of a string.

klabranche
Thanks, but the example above is not 'real world'. The field(s) I am thinking about is stuff like 'Description', and 'Address'. If they pass through null for those values, and then try to insert them into my database it will blow up. That is because those fields on my database are set to 'not null'. I guess I have some other options, I could make it convert the null values to empty strings, or indeed change my database structure to include null values for those fields. Not saying either of those options are good ones, but possible.
peter
Having said that I could make various changes to make this thing work, but it would still be easy for me to miss a field. I miss that field, and one day in 6 months time that is the first time they set that field to null. Bam, blow up time.
peter
I might be stating something you already know but be sure to look into IsNullOrEmpty to ease your null issue.... :-)
klabranche