Passing string parameter from Form1 to Form2

I found this piece of code on the forum that apparently worked for someone.  But it's confusing to me.
I want to pass a string from say Form1 to Form2.

Another option is to create a Class and store the variable there.  I have done that for an application I created and it works well.  For example:

Imports System

Public Class Global (whatever you want to call it)

Private Shared EmpID as String

Public Shared Property EmpIDVal as String
       Return EmpID
   End Get
   Set(ByVal Value As String)
       EmpID = Value
   End Set
End Property

To use in a form, send the value to this class by saying global.EmpIDVal = txtEmpID.Text.

In order to get the value in a form, use the following code:
Public Function ID() As String
        Return Global.EmpIDVal
    End Function

Hope this helps :-)

The generated code from my Form2 looks like this:

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

Are you able to use the 'Dim frm AS New Form2(text_value)' and modify the Form2_Load() above to accept this
text_value.  Or do you have to balloon across the Atlantic to do this....using code similar to the code I pasted
at the top.  This is a function that will be necessary for me in about (20) forms.  If it means setting up a Class
or something else that can be used globally..I'm all for it.  Just the same I've never created a separate Class
nor do I know where it would go.

My Module1.vb contains public variables as follows:

Imports System.Data
Imports System.Data.SqlClient

Module Module1
     Public this and that
End Module

If it can be added here...where do I add it.
Otherwise where and how would I add a Class to be used globally through the application and then how
would I suscessfully use it?

Thanks in advance!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

TSFLLCAuthor Commented:
I did get the create new Class code above to work.

However, what is more resourceful...or should I say efficient?

Create the class I included or just create a Global variable in Module1 and access it throughout the program?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It seems like you want to pass the string from Form1 to Form2 when you create Form2.

If so, then...

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim data As String = "data to pass to Form2"
        Dim f2 As New Form2(data)
    End Sub

End Class

Public Class Form2

    Private data As String

    Public Sub New(ByVal dataIn As String)
        InitializeComponent() = dataIn
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show(, "Data Passed In", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

End Class

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
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
What is more efficient?

That really depends on what you're doing...

Will you have more than one instance of Form2 that will require a different value to be passed to each?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

I can think of 2 quick ways you could do it
1. Using your Module1 to store the value as a global variable

Module Module1
  Public EmpID As String
End Module

In Form 1
Module1.EmpID = "1234"
' then Open Form 2

In Form 2
Dim EmpID As String = Module1.EmpID

2. Create an overrload in your form2 to accept the value as an argument

In Form2
Private _EmpID As String
Public Sub New(EmpID As String)
  _EmpId = EmpID
End Sub

_EmpId now stores the value

In Form1
Dim frm2 as New Form2("1234")

note: If you plan on using the 2nd approach - it'll add value to create a base form that includes this overload. Then Form2 will inherit from this form as follows

Class myBaseForm
  inherits Form
  Private _PassedValue As String
  Public Sub New (Value As String)
    _PassedValue = Value
  End Sub
End Class

Then in the form
Class Form2
  inherits myBaseForm

  Public Sub Click(..., ...) handles cmd.Click
  End Sub
End Class

oops - I didn't realize y'all figured it out already - I guess I had it open too long without refreshing...LOL!!!
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It's no biggie bchoor...I do it often as well.  =)
TSFLLCAuthor Commented:
Mike(Idle Mind),

I used your code and it works well.  Enjoy the points...

I'm assuming that I would need to add the 'Public Sub New()' code in each subsequent form
that requires the value to be passed?

What's more efficient...more instances of Form2 that require different values?

Yes, the value will be different for each form.  It's a unique record id for each table I store.
That's how I determine the record to display in all of my data entry forms.
I include this record id as the 1st column with width = 0 from a datagrid that contains table

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.