Global variables

  • Thread starter Thread starter Brian Cahill
  • Start date Start date
B

Brian Cahill

Hello,

I am trying to create a variable when my form loads which can be used
when I click my buttons. Here is my code. What am I doing wrong?
Thanks for any help.

Private Sub Connect_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
CreateObjConnect()
End Sub

Public Sub CreateObjConnect()
Dim objIco As New ICAClient
'Set Browser Protocol
objIco.BrowserProtocol = "UDP"

'Set Server locator
objIco.TCPBrowserAddress = "10.1.200.234"
'Disable IPCLaunch
objIco.IPCLaunch = False

'Set to launch, rather than embed session
objIco.Launch = True

'Enable seamless
objIco.TWIMode = True

'objIco.XmlAddressResolutionType = "IPv4-Port"
objIco.ConnectionEntry = "TEST"
objIco.WinstationDriver = "ICA 3.0"
objIco.TransportDriver = "TCP/IP"

'Set credentials
objIco.Username = "test"
objIco.SetProp("Password", "testtest")
objIco.Domain = "test"

'Specify application to launch
objIco.Application = "TEST"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
objIco.Connect()
End Sub
 
What is happening in your code is when the end of CreateObjConnect is
reach the garbage collection disposes your variables. Plus, the objlco
is a local veriable to the CreateObjConnect method. Saying that, I
wonder how come you didn't get a compiling error? don't you have option
explicit on?

Ahmed
 
Hello Brian,

You should read up on variable scope. You declare objIco in Sub CreateObjConnect()
(which by the way is not a very descriptive name for a method)... so when
CreateObjConnect() ends, your variable falls out of scope (ie, it's no longer
available and will be GC'd).

Declare your variable in the Class's globals section:

Public Class YourFormName

Private objIco As ICAClient = New ICAClient


Sub Connect_Load(blah, blah)
SetupICAClient()
End Sub

....

-Boo
 
i would like to add , that coding conventions tell us that




method scope variabels should be declared with Dim all other declarations
should be declared with there scope attribute
( eg private , friend , public )


regards

Michel Posseth [MCP]
 
Back
Top