pass class instance between forms

jmycr

New member
Joined
Jan 10, 2007
Messages
2
Programming Experience
5-10
I would like to pass a user defined class from one form to another.

There is a catch.

Form A is my main form and references a dll that contains Form B. The class that I wish to pass is a separate dll that is referenced only by Form A. This is what I have so far.

FORM A
'This is the class that I am referencing from another dll. It has several functions that I wish to use in Form B
Dim SQLConnection As UserConnectorClass = New UserConnectorClass

'This is me initiating FORM B. I pass both the current form and the class in the hopes that I can use at least one of them. I will eventually remove the one I can not.
Dim NewfrmDB As Connect.frmDB = New Connect.frmDB(Me, Me.SQLConnection)

FORM B
Public MainForm As Form
Public ConnectorClass As Object

Public Sub New(ByVal SubMainForm As Form, ByVal SubConnectorClass As Object)

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

MainForm = SubMainForm
ConnectorClass = SubConnectorClass

End Sub

I can see the SQLConnection class in the MainForm during runtime when i breakpoint however I have no idea in how to reference this class or the functions within. The form objects are available during compile time but not the SQLConnection class because it is not known at that time.

Anyone know how to do this?
 
Your FormB stores ConnectorClass as type Object, and is where UserConnectorClass instance is passed in. This is as far as it can go without reference to the type definition. You can't cast it to a type it does not have any knowledge of. FormB needs and has reference to UserConnectorClass also. If there is not used different connectors through FormB constructor you might as well declare this parameters strongly As UserConnectorClass. It is not the different forms of a project/assembly that holds the reference, but the project/assembly itself.

What you perhaps confuse is Imports? You might have written "Imports DllNamespace..." in FormA but not so far in FormB? When a assembly is referenced, you can make the Imports as shortcut for typing the full namespace qualified type. For example when DlllNamespace is imported you can declare variable as UserConnectorClass, without the Imports you would have to write variable As DllNamespace.UserConnectorClass
 
Back
Top