referencing existing object

A

AMC

Hi,

I know this is a simple question so please go easy on me, but anyway. I have
a class called 'Form1' that is initialized in sub main with the code:

Public Shared Sub Main()
Application.Run(New Form1)

End Sub

How do I refer to this already created class instance from inside another
class instead of creating a new instance of the class with :

Dim f As New Form1


I need to access it's methods from other classes

THanks!
 
H

Herfried K. Wagner [MVP]

* "AMC said:
I know this is a simple question so please go easy on me, but anyway. I have
a class called 'Form1' that is initialized in sub main with the code:

Public Shared Sub Main()
Application.Run(New Form1)

End Sub

How do I refer to this already created class instance from inside another
class instead of creating a new instance of the class with :

Dim f As New Form1

\\\
Public Module AppMain
Private m_AppMainForm() As MainForm

Public ReadOnly Property AppMainForm() As MainForm
Get
Return m_AppMainForm
End Get
End Property

Public Sub Main()
m_AppMainForm = New MainForm()
Application.Run(m_AppMainForm)
End Sub
End Module
///

You can access the main form from everywhere in the app by typing
'AppMain.AppMainForm'.
 
A

AMC

Thanks! that worked great

Herfried K. Wagner said:
\\\
Public Module AppMain
Private m_AppMainForm() As MainForm

Public ReadOnly Property AppMainForm() As MainForm
Get
Return m_AppMainForm
End Get
End Property

Public Sub Main()
m_AppMainForm = New MainForm()
Application.Run(m_AppMainForm)
End Sub
End Module
///

You can access the main form from everywhere in the app by typing
'AppMain.AppMainForm'.
 
C

Cor

Hi Herfried,

You know I do not use that appmain, but this looks for me as definitly the
best I have seen until now, my compliments.

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
You know I do not use that appmain, but this looks for me as definitly the
best I have seen until now, my compliments.

:)

When writing the code for the post, I was not sure if I should place
'Sub Main' in a class as a shared method or in a module, but then I
decided to use a module. So I don't have to type the 'Shared' keyword
for every member, and everything that belongs to the whole application
should (IMO) be shared. Implementing sort of singleton pattern would
work too, but in this case I decided to use the RAD approach ;-).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top