Howdy Anatoly and Yermo,
A few basic questions about when and where extension variables should be cleared. Please refer to the code block below.
1. Are variables which are declared inside of a function automatically destroyed or cleared when the function exits, such as Loc1, Loc2 and rtnCode?
2. When/where should global variables be cleared, such as G_MyValue1 and G_MyValue2? Just before the End Function statement for the OnMenuItemSelected function? Or somewhere else?
Thank you,
-Don
<pss_extension name="Basic Shell" version="1.0">This is a basic VBScript Shell
<author email="none@none.com" name="Don G." url="http://www.noURL.com">
<![CDATA[
' Basic Shell - A basic VBScript Shell extension for Personal Stock Monitor
' Copyleft © 2010 Don G. - All rights released.
]]>
</author>
<script language="VBScript">
<![CDATA[
Class MyFormHandler
public Function OnMenuItemSelected ( id )
If (id <> G_MyForm) Then
Exit Function
End If
Dim Loc1
Dim rtnCode
Loc1 = 1
G_MyValue1 = "G1"
G_MyValue2 = "n/a"
MsgBox "Loc1=" & Loc1 _
& " G_MyValue1=" & G_MyValue1 _
& " G_MyValue2=" & G_MyValue2, vbOKOnly, "Msg"
rtnCode = 0
DoSomething rtnCode
MsgBox "DoSomething() rtnCode=" & rtnCode, vbOKOnly, "Msg"
' *** Clear global variables here? ***
G_MyValue1 = ""
G_MyValue2 = ""
End Function
public Function DoSomething ( rtnCode )
Dim Loc2
Loc2 = 2
G_MyValue1 = "n/a"
G_MyValue2 = "G2"
MsgBox "Loc2=" & Loc2 _
& " G_MyValue1=" & G_MyValue1 _
& " G_MyValue2=" & G_MyValue2, vbOKOnly, "Msg"
rtnCode = 1
End Function
' Dimension global variables ...
Dim G_MyForm
Dim G_MyValue1
Dim G_MyValue2
End Class
' =======================================================
' Initialization ...
Set EventManager = Application.GetObject("EventManager")
Set MenuManager = Application.GetObject("MenuManager")
Set FormHandler = new MyFormHandler
' Create our custom menu entries ...
If (Not MenuManager Is Nothing) Then
Set MainMenu = MenuManager.MainMenu
nToolMenu = MainMenu.Find("Tools")
' Create the Tools top-level menu if it doesn't exist ...
If (nToolMenu = -1) Then
nHelpMenu = MainMenu.Find("Help")
Set ToolMenu = MainMenu.InsertMenu(nHelpMenu, "Tools")
Else
Set ToolMenu = MainMenu.GetSubMenu(nToolMenu)
End If
' Actually create the menu commands and save the ids for later ...
If (Not ToolMenu Is Nothing) Then
ToolMenu.InsertSeparator ToolMenu.ItemCount
FormHandler.G_MyForm = ToolMenu.InsertItem(ToolMenu.ItemCount, "Basic Shell")
End If
End If
' Register our event handlers ...
If (Not EventManager Is Nothing) Then
EventManager.RegisterHandlerMethod FormHandler, "OnMenuItemSelected"
Else
' Could not get EventManager object.
End If
]]>
</script>
</pss_extension>