Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing parameters to forms in VB.Net

Posted on 2003-10-30
10
Medium Priority
?
655 Views
Last Modified: 2008-01-09
I have a form (Form2) that I want to pass ByRef (from Form1) a parameter to and have the form (Form2) modify the parameter.  Is this possible???  Below is my own small script of the action (it does not represent the actual code).

Thanks in advance for any help,

Hammer



' Declare Form1
Form1

Dim strName As String

strName = "John"

Dim NewForm2 As New Form2(strName)

' strName should have changed from "John" to "Charley"

End Form1


' Declare Form2
Form2(ByRef strPrmNewName)

strPrmNewName = "Charley"

End Form2
0
Comment
Question by:HAMMER33333
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 9653285
yEAH JUST IN fORM 2

hAVE A Procedure called New()

And design as many parameters as needed

like

new(Blas as blah,Blas as blah)

And when you do AS new Form2(  you will get the parameters
0
 

Author Comment

by:HAMMER33333
ID: 9653363
How about this if Form2:

Public Sub New(ByRef strPrmNewName As String)
MyBase.New()

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

  'Add any initialization after the InitializeComponent() call

End Sub


on Form2 I have a button that calls a function that will update "strPrmNewName" so that when the form closes the ByRef setting will modify the parameter passed.  Is this possible???   I cannot seem to access "strPrmNewName" outside the "Sub New" and update it to something.

Thanks again,

Hammer
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9653409
so make a global Variable and then


Dim g_strPrmNewName  as String

New(strPrmNewName  as string)

g_strPrmNewName  =  strPrmNewName

end sub


Public sub whatever

  MSGBOX     G_strPrmNewName
0
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 

Author Comment

by:HAMMER33333
ID: 9653458
But the parameter was passed ByRef so how do you get the value passed from Form1 to change?   In the above example you are setting it to a global variable which is local to Form2 (Form1 does not know about it).  So g_strPrmNewName is getting set to "strPrmNewName" but "Form1.strName" which is passed is not.  Do you see what I mean?

I want to:

1. Pass a variable from Form1 to Form2

2. Modify the passed variable from Form1 to something new using a function in Form2.

3. The result will be that the Form1 variable will change after a call to Form2.

I don't even know if this is possible.

Sorry for not being clear,
Thanks for the help,

Hammer
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9653484
USe ByVal not ByREf
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9658341
Define a property and a module-level string as a holder:

rivate module_strName As String
   Public Property strName() As String
      Get
         Return module_strName
      End Get
      Set(ByVal Value As String)
         module_strName = Value
      End Set
   End Property
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9658353
Define a property and a module-level string as a holder:

Private module_strName As String

Public Property strName() As String
   Get
      Return module_strName
   End Get
   Set(ByVal Value As String)
      module_strName = Value
   End Set
End Property

In your code you can change the value of module_strName, and when accessed from outside the form through the property, the modified value will be returned to the caller.
0
 

Author Comment

by:HAMMER33333
ID: 9658477
TheLearnedOne,

I am still a Grasshopper.  How can I apply this to my Form2 call?  As in:

' Declare Form1
Form1

Dim strName As String

strName = "John"

Dim NewForm2 As New Form2(strName)
NewForm2.ShowDialog()

' strName should change from "John" to "Charley" if the user clicks the button in Form2

End Form1


' Declare Form2
Form2

Public Sub New(ByRef strPrmNewName As String)
MyBase.New()

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

  'Add any initialization after the InitializeComponent() call

End Sub

Sub btnSetValue_Click

strPrmNewName = "Charley"
' "strPrmNewName" is not know here in this subroutine

Me.Close

End Sub

End Form2


Thanks a bunch,
I believe we are getting close,

Hammer
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 80 total points
ID: 9658555
Okay, grasshopper.  Lesson #1:  Using the New constructor to set a variable is basically a one-way operation.  The code to create the new form sets the value.  Then there is nothing on the other end to receive the change, because the change to the value is after the form is initialized, and the New is no longer in the picture.

Lesson #2:  Unlike VB6, where the form was almost like a class, the form in VB.NET is exactly like a class, so you can add properties like you can in a Class definition.  In this way you can transfer values between two objects (Form1 and Form2).  Form2 can modify the internal value, and when Form1 asks for the value from Form2, then the modified value will be returned.

' Declare Form1
Form1

Dim strName As String

strName = "John"

Dim NewForm2 As New Form2()
NewForm2.strName = strName
NewForm2.ShowDialog()

' strName should change from "John" to "Charley" if the user clicks the button in Form2

End Form1


' Declare Form2
Form2

Private module_strName As String

Public Property strName() As String
   Get
      Return module_strName
   End Get
   Set(ByVal Value As String)
      module_strName = Value
   End Set
End Property

Sub btnSetValue_Click

   module_strName = "Charley"

Me.Close

End Sub

End Form2
0
 

Author Comment

by:HAMMER33333
ID: 9658686
TheLearnedOne,

       It works!   Except I had to add the "*" code to the Form1 call of Form2 below.  It works just like I wanted.

       You da man!!!

Thanks very much,

Hammer



Form1

        MsgBox("Before: " & strName)

        Dim NewForm2 As New Form2()
        NewForm2.strName = strName
        NewForm2.ShowDialog()
 *     If NewForm2.strName <> strName Then
 *         ' Set it only if it was modified
 *         strName = NewForm2.strName
 *     End If

        MsgBox("After: " & strName)

End Form1
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

718 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