简体   繁体   中英

C# checking for undefined variable received via querystring from JQuery/Ajax call

There are many questions discussing this topic with ref to Javascript; but I could not get any with ref to C#.

Both the 'String........' statements below return false .

     // foll querystring value from JQuery/Ajax call
     var  thisfieldvalue = Request.QueryString["fieldvalue"];

     bool boola = String.IsNullOrWhiteSpace(thisfieldvalue );
     bool boolb = String.IsNullOrEmpty(thisfieldvalue );

What is the best way to check for Undefined string variable in C#?

Note:
I get 'Undefined variable' values occasionally, via the JQuery/Ajax calls with the 'querystring'; and it ends up in the C# variable when I use the statement

var thisfieldvalue = Request.QueryString["fieldvalue"];

and the 'thisfieldvalue' variable passes both the 'String.IsNullOrWhiteSpace' as well as the 'String.IsNullOrEmpty' checks....

Note 2: I have edited the question again to make my question clearer... I am sorry that earlier it was not that clear....

you could use either

 string Undefined_var = "[value to test goes here]";  //note that string must be assigned before it is used
 bool boola = String.IsNullOrWhiteSpace(Undefined_var);

 //or     

 bool boolb = String.IsNullOrEmpty(Undefined_var);

Difference being that IsNullOrWhiteSpace will check everything that IsNullOrEmpty does, plus the case when Undefined_var consists of only white space. But since a string consisting of only white space characters is not technically undefined, I would go with IsNullOrEmpty of the two.

But do note that since string is a reference type, the default value is null ; so if you wanted to narrow down a step farther to eliminate the test for an empty string, you could do something like this-

 string Undefined_var = null;
 bool boola = Undefined_var == null;

There are no "undefined" string variables in C#.

String is a reference type, therefore if you don't define a value, it's default value is null .

There is no difference between a string not set to a value (default value null ) and a string explicitely set to null .

In Visual Studio 2013 your code doesn't even compile. The first check gets flagged as use of unassigned local variable .

As C# is a strongly typed language, use it to your advantage, set the value explicitly:

 string Undefined_var = null;
 bool boola = String.IsNullOrWhiteSpace(Undefined_var);
 bool boolb = String.IsNullOrEmpty(Undefined_var);

Then you will get two true values.

It question is not applicable to C# because C# does not allows a non-defined local variables. Members of classes are initialized by a member's default value (for reference types - initialized by null).

if (Request.QueryString["fieldvalue"] == "undefined")

It's a string, it will come across literally as a string.

If it is 5 it's a string of 5

If it is not there it's a string of undefined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM