Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2014-08-08
17
Medium Priority
?
152 Views
Last Modified: 2014-08-11
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.
0
Comment
Question by:BlakeMcKenna
  • 12
  • 5
17 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40249464
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
 

Author Comment

by:BlakeMcKenna
ID: 40249494
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
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 40249499
remove the New on your public property. you are not creating a new instance, you will reuse an existing one
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:BlakeMcKenna
ID: 40249509
Thanks Eric...that worked!
0
 

Author Comment

by:BlakeMcKenna
ID: 40253178
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40253209
it has been working for a while and now it stopped?
0
 

Author Comment

by:BlakeMcKenna
ID: 40253218
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40253349
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
 

Author Comment

by:BlakeMcKenna
ID: 40253405
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
 

Author Comment

by:BlakeMcKenna
ID: 40253415
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40253450
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
 

Author Comment

by:BlakeMcKenna
ID: 40253503
Still not working right.  See screenshots plz!
Screenshot1.jpg
0
 

Author Comment

by:BlakeMcKenna
ID: 40253505
0
 

Author Comment

by:BlakeMcKenna
ID: 40253511
disregard...I forgot to change how it was defined in Form2...testing now!
0
 

Author Comment

by:BlakeMcKenna
ID: 40253544
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
 

Author Comment

by:BlakeMcKenna
ID: 40253592
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
 

Author Comment

by:BlakeMcKenna
ID: 40253614
I have it working now...
0

Featured Post

Upgrade your Question Security!

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question