Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

All,

If I declare a new object, say, a Hashtable in Form.Load event, how can I access this object from another form? The worse is, I can't even see this object from other events in the SAME form. Does anyone have any clue?

 

Thanks,

Carl

Donald DUCK : YOU ARE FIRED!!!
Posted

Class level variables and properties

 

Variables declared within a method are local to that method only and not from other methods within the same class. To allow the variable to be used by other methods it must be declared at class level:

 

Public Class MyForm
   Inherits Form

   'Class-level variables
   Private m_htable As Hashtable

   'Intantiate within Form_Load
   Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
       'Use m_htable
       m_htable = New Hashtable()
   End Sub

   'Use within other methods
   Private Sub Form_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
       'Use m_htable
       m_htable.Clear()
   End Sub

End Class

 

To allow htable to be accessible from outside MyForm it should be exposed using a property. Unless you want other classes to be able to change the actual instance of htable, you should declare the property ReadOnly:

 

Public Readonly Property HTable As Hashtable
   Get
       Return m_htable
   End Get
End Property

 

Good luck :cool:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...