passing information between forms using a property

I have a class with several properties
on my main form when you select an item in a grid it populates a value into the property, but when I move to another form and try and use the value in the property it is empty.. not sure what I am doing wrong.. thanks for the help

Property
Public Class ProjectInfo
    Private strControlID As String
    Public Property strPubControlID() As String
        Get
            Return strControlID
        End Get
        Set(ByVal value As String)
            strControlID = value
        End Set
    End Property

    Private strPath As String
    Public Property psrPath() As String
        Get
            Return strPath
        End Get
        Set(ByVal value As String)
            strPath = value
        End Set
    End Property

    Private sProjID As String
    Public Property ProjID() As String
        Get
            Return sProjID
        End Get
        Set(ByVal value As String)
            sProjID = value
        End Set
    End Property


End Class

Open in new window

example of filling the property (I have tested that I can use the value on that page

Dim ProjID As ProjectInfo = New ProjectInfo
ProjID.ProjID = (DirectCast(RadGrid1.SelectedItems(0), GridDataItem)).GetDataKeyValue("KEYField").ToString()

Open in new window


here is where I try to use it on another form and it is empty

Dim Projectinfo As ProjectInfo = New ProjectInfo
Dim ProjId As String = Projectinfo.ProjID

Open in new window

David ModugnoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
If you are instantiating the ProjectInfo class in one form, then in order to use the ProjectInfo class in another form you have two options.

1.  Make the ProjectInfo instance a public property.
2.  Pass the ProjectInfo instance as a parameter to the new forms constructor.

For the first option, this is generally accomplished like this:
Form1.vb -
Public Class Form1
	Public Property [ProjectInfo]() As ProjectInfo


	Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		ProjectInfo = New ProjectInfo()
		ProjectInfo.ProjID = "Form1_ProjectInfo"
	End Sub

	Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
		Dim frm = New Form2()
		frm.ShowDialog()
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(13, 13)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(75, 23)
		Me.Button1.TabIndex = 0
		Me.Button1.Text = "Load Form2"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.Button1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

Open in new window

Form2.vb -
Public Class Form2

	Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		TextBox1.Text = Form1.ProjectInfo.ProjID
	End Sub
End Class

Open in new window

Form2.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form2
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.TextBox1 = New System.Windows.Forms.TextBox()
		Me.SuspendLayout()
		'
		'TextBox1
		'
		Me.TextBox1.Location = New System.Drawing.Point(13, 13)
		Me.TextBox1.Name = "TextBox1"
		Me.TextBox1.Size = New System.Drawing.Size(100, 20)
		Me.TextBox1.TabIndex = 0
		'
		'Form2
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.TextBox1)
		Me.Name = "Form2"
		Me.Text = "Form2"
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class

Open in new window

ProjectInfo.vb -
Public Class ProjectInfo
	Public Property strPubControlID() As String
	Public Property psrPath() As String
	Public Property ProjID() As String
End Class

Open in new window

Which produces the following output -Form1 loads.  So we press the form1 button.Form2 loads showing the projectinfo id that was set in form 1.-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
For the second option, we would implement a constructor that accepted a ProjectInfo object as a parameter.  Something like:

Form1.vb -
Public Class Form1
	Private Property [ProjectInfo]() As ProjectInfo

	Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		ProjectInfo = New ProjectInfo()
		ProjectInfo.ProjID = "Form1_ProjectInfo"
	End Sub

	Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
		Dim frm = New Form2(ProjectInfo)
		frm.ShowDialog()
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(13, 13)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(75, 23)
		Me.Button1.TabIndex = 0
		Me.Button1.Text = "Load Form2"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.Button1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

Open in new window

Form2.vb -
Public Class Form2
	Private [ProjectInfo] As ProjectInfo

	Public Sub New(ByVal [ProjectInfo] As ProjectInfo)
		Me.ProjectInfo = ProjectInfo
		InitializeComponent()
	End Sub

	Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		TextBox1.Text = ProjectInfo.ProjID
	End Sub
End Class

Open in new window

Form2.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form2
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.TextBox1 = New System.Windows.Forms.TextBox()
		Me.SuspendLayout()
		'
		'TextBox1
		'
		Me.TextBox1.Location = New System.Drawing.Point(13, 13)
		Me.TextBox1.Name = "TextBox1"
		Me.TextBox1.Size = New System.Drawing.Size(100, 20)
		Me.TextBox1.TabIndex = 0
		'
		'Form2
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.TextBox1)
		Me.Name = "Form2"
		Me.Text = "Form2"
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class

Open in new window

Which will produce the same output as above.

-saige-
0
David ModugnoAuthor Commented:
I have to admit I am new to this and not really following you... not sure if it matters, but this is an asp.net solution with vb.net code behind.

Couple of ?'s
if you look at my ProjectInfo Class are the properties setup correctly and should they be in their own class or should they be on one of the forms?

when I defined the Properties I thought that makes them public

Thanks again for your help
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Jacques Bourgeois (James Burger)PresidentCommented:
Whenever you call New on a class, you create a new object.

In your "another form", you do Dim Projectinfo As ProjectInfo = New ProjectInfo

You are thus creating a completely new object that has nothing to do with the first one. That is why your property does not return anything.

Here is an article from Microsoft that shows how to pass information between pages of an asp.net application.
0
AndyAinscowFreelance programmer / ConsultantCommented:
As pointed out in the previous comment it is creating a new instance of the class that is the problem.
Just in case you don't understand think of putting something such as a book into a bag.  Now pick up another bag that looks the same.  Does that contain the book?  You'd be very surprised if it did.

ps.  The public is to do with how it can be accessed, not your problem at all.
0
it_saigeDeveloperCommented:
I sort of glazed over what James and Andy have pointed out.  It's not whether your accessors are public or private in this case, it's because you create an instance in one form and then try to create a new instance in a seperate form.

My example still creates the instance in one form, but makes the instance available from the form (option 1) or passes the instance from one form to the other (option 2).  As you are using ASP.NET instead of Windows Forms, the theory is the same, the implementation details are different (as James has pointed out).

-saige-
0
David ModugnoAuthor Commented:
Thanks for the help... working great
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.