ASP会话变量:是“".与IsEmpty一样吗? [英] ASP Session variables: Is "" same as IsEmpty?

查看:118
本文介绍了ASP会话变量:是“".与IsEmpty一样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP中,未初始化的会话变量为空.我知道检查会话值并删除值的正确方法如下:

In ASP an uninitialized Session variable Is Empty. I know that the correct way to check for a Session value, and remove a value, is the following:

IF NOT IsEmpty(Session("myVar")) THEN
  ' Go ahead and use Session("myVar")
  ...
  ' Now if we're all done with myVar then remove it:
  Session.Contents.Remove("myVar")
END IF

我继承了一个代码库,其中使用后通常将Application和Session变量设置为= "",并且对值的所有测试均采用(Sessions("myVar") = "")形式.在未声明Session变量的情况下,此测试似乎可以正常工作.

I've inherited a codebase where Application and Session variables are typically set = "" after use, and all tests for a value are of the form (Sessions("myVar") = ""). This test appears to work when the Session variable has not been declared ... or maybe it's just working by dumb luck.

使用与空字符串的比较来测试Session变量是否安全?也就是说,以下正确"是否与上面显示的正确方法一样?

IF Session("myVar") <> "" THEN
  ' Go ahead and use Session("myVar")
  ...
  ' Now if we're all done with myVar then blank it:
  Session("myVar") = ""
END IF

或者我应该重构代码库,以便:

Or should I refactor the codebase so that:

  1. 所有用于确定是否已设置Session变量的测试均采用IsEmpty(Session("myVar"))
  2. 的形式
  3. 所有会话变量都是Remove d且未设置= ""?
  1. All tests to determine whether a Session variable has been set are of the form IsEmpty(Session("myVar"))
  2. All session variables are Removed and not set = ""?

推荐答案

Empty是一种奇怪的野兽:它同时等于""0.认真尝试:

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)

它将写出"True".

It'll write out "True".

这意味着对Not IsEmpty(myvar)的测试等同于对myvar <> ""的测试,但是IsEmpty(myvar)等同于myvar = "".无论是理论上的差异是否困扰您,只有您可以回答,但就我个人而言,我不会在重构上浪费时间.

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.

如果您决定进行重构,建议不要使用IsEmptyIsNull等,而只需使用& """hack"即可:

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

这将透明地处理Nulls Empties,而无需编写一堆代码.

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

这篇关于ASP会话变量:是“".与IsEmpty一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆