Get public properties from different forms.

Hi Experts,

I want to send parameter from one form to another. For example I have 3 forms. Form1, Form2 and Form3. Sometimes Form1 gets parameters from Form2 and sometimes it gets parameters from Form3. How can I make the Form name dynamic in the sub "New".

In this example I can get parameters from From2. What to  do to get parameters from different forms.
Public Sub New(ByVal formParameter As Form2)
        InitializeComponent()
        UsersObj = formParameter
    End Sub
RadhaKrishnaKiJayaAsked:
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:
Well assuming all of your forms inherit from the Form class, then you could change the call to something like:
Private UserObj As Form

Public Sub New(ByVal formParameter As Form)
	InitializeComponent()
	UserObj = formParameter
End Sub

Open in new window

Then when you need to access a property you just check for the type of form and cast appropriately:
Private Sub SomeMethod()
	If TypeOf UserObj Is Form2 Then
		DirectCast(UserObj, Form2).SomeForm2Property = SomeValue
	ElseIf TypeOf UserObj Is Form3 Then
		DirectCast(UserObj, Form3).SomeForm3Property = SomeValue
	End If
End Sub

Open in new window

But there is probably a better way to do what you need to do.  What are you ultimately trying to accomplish.

-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
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sometimes Form1 gets parameters from Form2 and sometimes it gets parameters from Form3.

But is the TYPE of those parameters the same from Form2 and Form3?  For instance, if you are receiving a String from either one, then change your Constructor to:
    Public Sub New(ByVal formParameter As String)
        InitializeComponent()
        UsersObj = formParameter
    End Sub

Open in new window


Then instead of sending the whole Form, just send the parameter from that Form.  If you want to send more than one value then create a Class to hold all the values and pass that.

If all the Forms being passed have the same property names and values, then make them Implement an Interface and change the Constructor to receive a reference of that Interface Type.
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much.
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.