This is the Personal Stock Monitor scripting developers forum.
Subscribe to RSS Feed
Developing Custom Stock Market Software using PSM -> Clearing Variables In Extensions
Not logged in.
\n\n\n[/code]\n");
2010-12-06 04:37:38
1 of 7
#2700
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>


Posted by: dgoyette
2010-12-06 08:29:39
2 of 7
#2701
in reply to #2700
1. Yes, that's basic local scoping rules.

2. Class variables don't get destroyed until the object is destroyed, unless you explicitly set them to Nothing. If you set them to any other value, such as "" or 0, where you do that is up to you, depending on what you want to accomplish. There is nothing that says that they must be cleared or destroyed explicitly.
Posted by: Anatoly
2010-12-06 14:14:53
3 of 7
#2704
in reply to #2701
Anatoly wrote
1. Yes, that's basic local scoping rules.

Thank you Anatoly, just wanted to be sure.

Anatoly wrote
2. Class variables don't get destroyed until the object is destroyed, unless you explicitly set them to Nothing. If you set them to any other value, such as "" or 0, where you do that is up to you, depending on what you want to accomplish. There is nothing that says that they must be cleared or destroyed explicitly.

So, where I have ...
'   ***  Clear global variables here?  ***
    G_MyValue1 = ""
    G_MyValue2 = ""
  End Function

... is this where the extension code ends? If so, and I want to destroy the variables, then the code should be as follows? ...
    Set G_MyValue1 = Nothing
    Set G_MyValue2 = Nothing

Thank you Anatoly,

-Don

Posted by: dgoyette
2010-12-06 14:54:13
4 of 7
#2705
in reply to #2704
dgoyette wrote

So, where I have ...
'   ***  Clear global variables here?  ***
    G_MyValue1 = ""
    G_MyValue2 = ""
  End Function

... is this where the extension code ends?


No, that's where the function OnMenuItemSelected ends. The extension actually ends right before the ]]> symbol.

dgoyette wrote
If so, and I want to destroy the variables, then the code should be as follows? ...
    Set G_MyValue1 = Nothing
    Set G_MyValue2 = Nothing


If you wanted to destroy the variables, and that's the right thing to do in that spot, then yes, you set them to Nothing. However keep in mind the error you got before when you set a variable to Nothing, because you were accessing the variable later and expecting it to exist. In general, setting a variable to Nothing isn't necessary in VBScript, unless you have a specific reason to do it such as explicitly freeing a reference to an object.

Posted by: Anatoly
2010-12-08 02:58:04
5 of 7
#2724
in reply to #2705
Yes - but, whatever code I place "right before the ]]> symbol", will only get executed when the extension is first run (initialization), BEFORE any of the functions are executed. So, this would not be a very good place to destroy global variables, right?
Posted by: dgoyette
2010-12-08 08:33:35
6 of 7
#2727
in reply to #2724
Yes, that code only runs when the extension loads, so you probably don't want to destroy global variables there since it's likely that you just created them. But you where asking where the extension ends, and that's where it ends. The code you had referenced was where the OnMenuItemSelected function ends.

As I said, unless you have a specific reason to do so, you don't need to destroy global variables in your extension.
Posted by: Anatoly
2010-12-08 17:34:53
7 of 7
#2738
in reply to #2727
Thank you for your clarification Anatoly.
Posted by: dgoyette