加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VB中清除变量

(2010-11-24 09:46:10)
标签:

杂谈

分类: QTP/VBS20100524

资料来源于网络

变量一但定义,就会在程序执行在内存开辟出相应的空间,清除变量除非程序重新定义变量,重新加载,但这不可能实现。
楼上说的是重置变量,即刚定义的初值,如:
字符变量为""
数值变量为0
布尔变量为False
对象变量可以Set 变量名=Nothing(如数据库对象) 
sub里面的变量在sub执行完后自动就清空了! 

1、定义对象变量可以加New,定义其它变量不能加New
2、你理解的基本正确,其实就是要释放对象变量所占的内存空间需要set nothing
3、VB下有指针,只是不提倡使用罢了,所以VB的帮助没有提到指针。
  VB下调用API不当可能会引起内存泄漏。

  是不是new出来的东西没有set nothing就会造成内在泄漏?
  不是,只是占用的内存空间不会释放而已。
 

Function printAB
   Dim a, b : a =10 : b="abc"
   'print "A: ";  a ' QTP not support  semicolon
  print ' QTP not support 
  print "B:", b ' QTP not support 

Print a
Print b
End Function

 

Print "--Test dim"
PrintAB

Print "Print the dim outside the function"
print a
print b

 

' Output log

'--Test dim
'10
'abc
'--Print the dim outside the function

 

Set Statement

Requirements

Version 1

Assigns an object reference to a variable or property, or associates a procedure reference with an event.

Set objectvar = {objectexpression | New classname | Nothing}

-or-

Set object.eventname = GetRef(procname)

Parameters

objectvar
Required. Name of the variable or property; follows standard variable naming conventions.
objectexpression
Optional. Expression consisting of the name of an object, another declared variable of the same object type, or a function or method that returns an object of the same object type.
New
Keyword used to create a new instance of a class. If objectvar contained a reference to an object, that reference is released when the new one is assigned. The New keyword can only be used to create an instance of a class.
classname
Optional. Name of the class being created. A class and its members are defined using the Class statement.
Nothing
Optional. Discontinues association of objectvar with any specific object or class. Assigning objectvar to Nothing releases all the system and memory resources associated with the previously referenced object when no other variable refers to it.
object
Required. Name of the object with which event is associated.
event
Required. Name of the event to which the function is to be bound.
procname
Required. String containing the name of the Sub or Function being associated with the event.

Remarks

To be valid, objectvar must be an object type consistent with the object being assigned to it.

The Dim, Private, Public, or ReDim statements only declare a variable that refers to an object. No actual object is referred to until you use the Set statement to assign a specific object.

Generally, when you use Set to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created. More than one object variable can refer to the same object. Because these variables are references to (rather than copies of) the object, any change in the object is reflected in all variables that refer to it.

Function ShowFreeSpace(drvPath)
   Dim fso, d, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set d = fso.GetDrive(fso.GetDriveName(drvPath))
   s = "Drive " & UCase(drvPath) & " - " 
   s = s & d.VolumeName  & "<BR>"
   s = s & "Free Space: " & FormatNumber(d.FreeSpace/1024, 0) 
   s = s & " Kbytes"
   ShowFreeSpace = s
End Function

Using the New keyword allows you to concurrently create an instance of a class and assign it to an object reference variable. The variable to which the instance of the class is being assigned must already have been declared with the Dim (or equivalent) statement.

Refer to the documentation for the GetRef function for information on using Set to associate a procedure with an event.

 

GetRef Function

Requirements

Version 5

Returns a reference to a procedure that can be bound to an event.

Set object.eventname = GetRef(procname)

Arguments

object
Required. Name of the object with which event is associated.
event
Required. Name of the event to which the function is to be bound.
procname
Required. String containing the name of the Sub or Function procedure being associated with the event.

Remarks

The GetRef function allows you to connect a VBScript procedure (Function or Sub) to any available event on your DHTML (Dynamic HTML) pages. The DHTML object model provides information about what events are available for its various objects.

In other scripting and programming languages, the functionality provided by GetRef is referred to as a function pointer, that is, it points to the address of a procedure to be executed when the specified event occurs.

The following example illustrates the use of the GetRef function.

<SCRIPT LANGUAGE="VBScript">

Function GetRefTest()
   Dim Splash
   Splash = "GetRefTest Version 1.0"   & vbCrLf
   Splash = Splash & Chr(169) & " YourCompany 1999 "
   MsgBox Splash
End Function

Set Window.Onload = GetRef("GetRefTest")
</SCRIPT>

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有