Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB vs JS

Status
Not open for further replies.

booboo0912

Programmer
Jul 31, 2002
75
US
Hello...can anyone tell me what the equivalent JavaScript code would be for the following:

set rs = nothing

Would it be:

rs = "";

or:

rs = null;

Is there a difference in using "" and null?

Thanks!
 
Interesting question.

Code:
<html>
<head>
	<title>Is null nothing?</title>
</head>

<body>

<script>
document.write(&quot;<br><br>JavaScript<br>&quot;);
var rs;
document.write(&quot;rs: &quot; + rs + &quot;<br>&quot;);
rs = &quot;&quot;;
document.write(&quot;rs: &quot; + rs + &quot;<br>&quot;);
document.write(&quot;rs.length: &quot; + rs.length + &quot;<br>&quot;);
document.write(&quot;null: &quot; + null + &quot;<br>&quot;);
rs = null;
document.write(&quot;rs: &quot; + rs + &quot;<br>&quot;);
document.write(&quot;rs.length: &quot; + rs.length + &quot;<br>&quot;);
</script>

<%
Response.write(&quot;<br><br>JScript<br>&quot;);
var rs;
Response.write(&quot;rs: &quot; + rs + &quot;<br>&quot;);
rs = &quot;&quot;;
Response.write(&quot;rs: &quot; + rs + &quot;<br>&quot;);
Response.write(&quot;rs.length: &quot; + rs.length + &quot;<br>&quot;);
Response.write(&quot;null: &quot; + null + &quot;<br>&quot;);
rs = null;
Response.write(&quot;rs: &quot; + rs + &quot;<br>&quot;);
Response.write(&quot;rs.length: &quot; + rs.length + &quot;<br>&quot;);
%>
</body>
</html>

The two lines attempting to write the length of null will give errors.

Apparently null is a symbol that stands for nothing. Oddly we can see null but we can't see &quot;&quot;, which is something; it is a string of length zero.

Regarding the use of
Code:
Set rs = nothing
and
Code:
rs = &quot;&quot;;
I would think they are not equivalent based on the above demonstration. You might add similar code in VBScript to further compare the two statements.

I write in JScript and use rs = &quot;&quot;; to clear the recordset object and recover memory. Since an empty string object doesn't use much memory that should be good enough. But your question leads me to think maybe I should start writing rs = null;

 
There is a difference between &quot;&quot; and Nothing/Null (VB/JS). But the difference may not have that big of an effect.

The whole point to setting an object to nothing is to allow the garbage collection to come along, note that no one is referencing it and (if no one is) clean it up.

If an object was something and has been set to nothing the something is still floating around in the system somewhere, but with no one pointing to it. Since no one is pointing to it it should get cleaned up.

In summary:
You have set the variable to something, just a different something than the current something. The fact that it is not pointing to nothing is completely irrelevant since the something it is pointing to is nearly nothing and the something it was pointing to is being refered to by nothing.

I think.

_______________________________________
Ignorance is a beautiful thing:
You don't know what you can't do...
 
I am not sure how this affects the VB garbage collection mechanism, but in case it helps I do know how this would work in a Java (not Javascript ...JAVA) environment.

In java, setting a String object to &quot;&quot; is called an 'empty string' ...there may be no characters assigned to this string, but it is still an object which has been instantiated. This means it's constructor has been called, it takes up 'object space' in memory, a check to see if it is null will return false, and it will not be garbage collected if any references to this string remain in scope.

Setting a string to null, however, means it is truly null ...it is nothing, it is not constructed yet, it takes up no memory, and it is not a 'live' object.

I guess another way to say this is that an empty string (&quot;&quot;) is a valid string ...its just a string with 'no characters in it' Empty strings are nice in that they won't crash visual components like a string set to null may do.

I hope this adds to your discussion ...in Java there is a difference ...a big one! One is an object, the other is nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top