2

I have given a web service Url. One of its methods have an argument that if I pass null it does something and if pass int does something else. I have added a service reference to project. C# does not accept passing null to that argument because it has implemented its interface to accept int and not int? (nullable int).

Please advise.

Fariborz Navidan
  • 91
  • 1
  • 2
  • 9

1 Answers1

0

You can also do an horrible walkarround, parsing int.MinValue or int.MaxValue. I really don't like that solution and it's not a best practice, but could do the trick.

for exeample

public void DoSomething( int numofSomething )
{
    if ( numofSomething == int.MinValue )
    {
        // act as if numofSomething is null
    }
    else
    {
        // act as normal
    }
}

Keep in mind you are doing your code more horrible, because you are using a minvalue or maxvalue to represent a null. Do this only if changing the interface is not an option.

Jordi
  • 2,789
  • 1
  • 20
  • 35
  • Hi. I am not coder of web service.It is somewhere on the web and I should just call it. I tried to change the definition of the method in the generated interface and change int parameter to int? but it raised an implementation error. – Fariborz Navidan Dec 01 '12 at 15:03
  • 1
    If the webservice expects an int, you will not be able to pass a null. In case you found some walkarround, you will provoke the error on the webservice host. If it's not prepared to work with nulls, I am affraid you cannot walkarround it. :( – Jordi Dec 01 '12 at 15:11
  • They have themselves proposed that I should pass either null or a timestamp as int. In PHP I can easily pass null using nusoap however I am stuck in C# implementation. – Fariborz Navidan Dec 01 '12 at 15:17
  • timestamp as int? maybe they consider the parameter as null if it's bigger that some number. Maybe you can ask and simply pass something like a really big number and hope the webservice will take it as a null. Anyway, looks really weird and a bad practice. – Jordi Dec 01 '12 at 15:19
  • It is a sms web service. I should pass null for sending right now or pass a timestamp for scheduling. – Fariborz Navidan Dec 01 '12 at 15:33
  • Pass the timestamp representing now? – Jordi Dec 01 '12 at 15:37
  • So please give me code fpr converting DateTime.Now to Unix timestamp in UTC – Fariborz Navidan Dec 01 '12 at 15:51
  • http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa – Jordi Dec 01 '12 at 16:06