How do I pass a Class from one Form to another Form?

What I'm trying to do is simple but it still eludes me. I simply want to pass a Class from Form1 to Form2, populate some properties and then pass them back to Form1 to be referenced there. My class looks like this.

Public Class Comments
      Property comment_ID As Integer
      Property comment As String
End Class

Open in new window


I need to be able to check the Class values once they are passed back to Form1.
BlakeMcKennaAsked:
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.

Éric MoreauSenior .Net ConsultantCommented:
create a public property in your Form2 of type Comments and then from your Form1:
dim x as new Form2
x.CommentsProperty = Form1CommentsInstance

Because only the pointer to instance is copied, setting this object in Form2 will also affect the object in Form1
0
BlakeMcKennaAuthor Commented:
Ok,

In my calling form, I declare the Class: clsComments like this:

Private clsComments As New CalTableClass.Calibration.Comments

The Class I defined in my first post is contained in a Project called CalTableClass.


So...in my called Form (Form2), I declare a property like so:

Public Property comment AS New CalTableClass.Calibration.Comments.

Is this correct?
0
Éric MoreauSenior .Net ConsultantCommented:
remove the New on your public property. you are not creating a new instance, you will reuse an existing one
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

BlakeMcKennaAuthor Commented:
Thanks Eric...that worked!
0
BlakeMcKennaAuthor Commented:
Eric,

For some reason it's not working now. I did what you said and showed in your example. Here is my code below.

This is the code for Form1
    Private Sub btnComments_Click(sender As Object, e As EventArgs) Handles btnComments.Click
        Try
            Dim frm As New frmTestComments

            frm.ShowDialog()

            InitializeErrorClass(EH)
            SaveCommentData(EH)

        Catch ex As Exception
            EH.ErrorMessage = "btnComments_Click() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window


This is the code for Form2
    Public clsC As CalTableClasses.CalibrationClasses.Comments    'Declaration of Class Variable in Form2


    Private Sub btnNewTestComment_Click(sender As Object, e As EventArgs) Handles btnNewTestComment.Click
        Try
            Dim Point1 As New Point(Me.Left, Me.Top)
            Dim strNote As String = InputBox("Enter Comment", "Internal Note", "", Point1.X + txtInternalNotes.Width + 50, Point1.Y + txtInternalNotes.Top + 330)

            txtInternalNotes.Text &= strNote & vbCrLf & vbCrLf
            clsC.comment = txtInternalNotes.Text     'Object reference not set to an instance of an object Error
            clsC.madeChanges = True

        Catch ex As Exception
            EH.ErrorMessage = "btnNewTestComment_Click() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
it has been working for a while and now it stopped?
0
BlakeMcKennaAuthor Commented:
I don't what happened...perhaps I never got it working in the first place and was looking at something else. The above code is what I have right now...
0
Éric MoreauSenior .Net ConsultantCommented:
just created a sample and it is working:

Public Class Form1

    Private clsComments As New Comments

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'init with default values
        clsComments.comment_ID = 1
        clsComments.comment = "value from form 1"

        Dim frm2 As New Form2
        frm2.clsCommentsF2 = clsComments
        frm2.ShowDialog()

        MessageBox.Show(clsComments.comment_ID.ToString() + " - " + clsComments.comment)

    End Sub
End Class


Public Class Form2

    Public clsCommentsF2 As Comments

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        clsCommentsF2.comment_ID = 2
        clsCommentsF2.comment = "modified from form 2"
        Close()
    End Sub
End Class



Public Class Comments
    Property comment_ID As Integer
    Property comment As String
End Class

Open in new window


That means that you have something in your setup which breaks the thing
0
BlakeMcKennaAuthor Commented:
I'm still not able to get it working. Here is all my code pertaining to this functionality for Form1 and Form2.

Code for Form1
    Private Sub btnComments_Click(sender As Object, e As EventArgs) Handles btnComments.Click
        Try
            Dim frm As New frmTestComments

            frm.clsC = clsComments
            frm.ShowDialog()

            InitializeErrorClass(EH)
            SaveCommentData(EH)

        Catch ex As Exception
            EH.ErrorMessage = "btnComments_Click() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
    '
    '
    '
    Private Function SaveCommentData(ByRef EH As ErrorHandling.ErrorHandler)
        Try
            EH.ErrorMessage = ""

            gMain_ID = iMain_ID

            ReDim Preserve clsComments(idxComment)

            clsComments(idxComment) = New CalTableClasses.CalibrationClasses.Comments
            clsComments(idxComment).main_ID = gMain_ID
            clsComments(idxComment).comment_ID = frmTestComments.clsC(idxComment).comment_ID
            clsComments(idxComment).seqNO = frmTestComments.clsC(idxComment).seqNO
            clsComments(idxComment).madeChanges = frmTestComments.clsC(idxComment).madeChanges
            clsComments(idxComment).comment = frmTestComments.clsC(idxComment).comment
            clsComments(idxComment).internal = frmTestComments.clsC(idxComment).internal
            clsComments(idxComment).certificate = frmTestComments.clsC(idxComment).certificate
            clsComments(idxComment).createdBy_ID = gID
            clsComments(idxComment).active = True
            clsComments(idxComment).updatedBy_ID = gID

            idxComment += 1

        Catch ex As Exception
            EH.ErrorMessage = "btnComments_Click() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Function

Open in new window



Code for Form2
    Public clsC() As CalTableClasses.CalibrationClasses.Comments


    Private Sub btnInternalComment_Click(sender As Object, e As EventArgs) Handles btnInternalComment.Click
        Try
            Dim Point1 As New Point(Me.Left, Me.Top)
            Dim strNote As String = InputBox("Enter Comment", "Internal Note", "", Point1.X + txtInternalNotes.Width + 50, Point1.Y + txtInternalNotes.Top + 330)

            txtInternalNotes.Text &= strNote & vbCrLf & vbCrLf

            ReDim Preserve clsC(idx)

            clsC(idx) = New CalTableClasses.CalibrationClasses.Comments
            clsC(idx).comment = txtInternalNotes.Text
            clsC(idx).madeChanges = True

            idx += 1

        Catch ex As Exception
            EH.ErrorMessage = "btnInternalComment_Click() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
BlakeMcKennaAuthor Commented:
I've receiving the "Object reference not set to an instance of an object error".

    Private Function SaveCommentData(ByRef EH As ErrorHandling.ErrorHandler)
        Try
            EH.ErrorMessage = ""

            gMain_ID = iMain_ID

            ReDim Preserve clsComments(idxComment)

            clsComments(idxComment) = New CalTableClasses.CalibrationClasses.Comments
            clsComments(idxComment).main_ID = gMain_ID
            clsComments(idxComment).comment_ID = frmTestComments.clsC(idxComment).comment_ID   'Getting reference error on this line
            clsComments(idxComment).seqNO = frmTestComments.clsC(idxComment).seqNO
            clsComments(idxComment).madeChanges = frmTestComments.clsC(idxComment).madeChanges
            clsComments(idxComment).comment = frmTestComments.clsC(idxComment).comment
            clsComments(idxComment).internal = frmTestComments.clsC(idxComment).internal
            clsComments(idxComment).certificate = frmTestComments.clsC(idxComment).certificate
            clsComments(idxComment).createdBy_ID = gID
            clsComments(idxComment).active = True
            clsComments(idxComment).updatedBy_ID = gID

            idxComment += 1

        Catch ex As Exception
            EH.ErrorMessage = "btnComments_Click() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Function

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
I have just seen that you are dealing with arrays and you are redimming it from form2? this is the issue. Replace it with a List (of T):

Public Class Form1

    Private clsComments As New List(Of Comments)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim frm2 As New Form2
        frm2.clsCommentsF2 = clsComments
        frm2.ShowDialog()

        MessageBox.Show(clsComments(0).comment_ID.ToString() + " - " + clsComments(0).comment)
    End Sub
End Class

Public Class Form2

    Public clsCommentsF2 As List(Of Comments)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        clsCommentsF2.Add(New Comments() With {.comment_ID = 2, .comment = "value from form 2"})

        Close()
    End Sub
End Class

Open in new window

0
BlakeMcKennaAuthor Commented:
Still not working right.  See screenshots plz!
Screenshot1.jpg
0
BlakeMcKennaAuthor Commented:
0
BlakeMcKennaAuthor Commented:
disregard...I forgot to change how it was defined in Form2...testing now!
0
BlakeMcKennaAuthor Commented:
Ok,

So far syntactically I have it working. Now, how do I loop thru a List (Of Comments)...assuming more than 1 comment has been added?

Thanks!
0
BlakeMcKennaAuthor Commented:
Plus...I'm passing clsComments into my DataLayer and it doesn't like how I've defined it in the signature.

I imported the class into my datalayer. This is really getting difficult. To the point where I just want to use Global variables.
Screenshot1.jpg
0
BlakeMcKennaAuthor Commented:
I have it working now...
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.