ASP Session variables: Is “” same as IsEmpty?

一世执手 提交于 2019-12-05 14:58:41

Empty is a strange beast: it is simultaneously equal to both "" and 0. Seriously, try it:

dim x, y, z
x = Empty
y = ""
z = 0
Response.Write (x = y) AND (x = z)

It'll write out "True".

This means that testing for Not IsEmpty(myvar) is equivalent to testing myvar <> "", but IsEmpty(myvar) is not equivalent to myvar = "". Whether that mostly-theoretical difference bothers you or not is something only you can answer, but personally, I wouldn't waste time on refactoring.


If you do decide to refactor, I would suggest forgetting about IsEmpty and IsNull and whatnot, and just using the & "" "hack":

If Session("myvar") & "" <> "" Then

This'll transparently handle Nulls and Empties without you needing to write a whole bunch of code.

Zam

No, it could be not safe. Perhaps you need to use functions: IsNull, IsEmpty and VarType

IsNull -- returns True if expression is Null, that is, it contains no valid data; otherwise, IsNull returns False. If expression consists of more than one variable, Null in any constituent variable causes True to be returned for the entire expression.

VarType -- Returns a value indicating the subtype of a variable.

IsEmpty -- returns True if the variable is uninitialized, or is explicitly set to Empty; otherwise, it returns False. False is always returned if expression contains more than one variable.

Please take a look at What is the '<>' asp operator?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!