How to write the New() method when a form is called by two different forms

RadhaKrishnaKiJaya
RadhaKrishnaKiJaya used Ask the Experts™
on
Hello Experts,

I have a form lets say "Form A". I have two different forms called "Form B"  and "Form C". Now my program(Form B)  is calling "form A"  with some parameters. I have defined a New() method.

Now my requirement is that Form C should also call Form A with some parameters.  What can I do so that the New() method will understand which form is calling.  

Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
You can have two methods with the same name but with different parameter lists.
eg.
New(int i)
and
New(String s, int j)


OR
You could just pass something to identify which type the calling form is as a parameter
eg.
New(int TypeOfForm)
{
switch(TypeOfForm)
{
case 1:
....
case 2:
....

Author

Commented:
Thank you for your reply.

Right now  my New function looks like this.
Public Sub New(ByVal formParameter As StockSearch) ''Where stockSearch is the name of the form
        InitializeComponent()
        StockSearchObj = formParameter
     
    End Sub
So can you please tell me how to change the code.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Make sure that you have a class variable like so to hold the value.

Dim StockSearchObj As StockSearch

Otherwise it looks good. Are you having issues with it?
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.

Author

Commented:
Thanks for ur reply.  This works fine when the stocksearch.vb call the frmModify.vb. Now I want RackStatus.VB should call the same frmModify.vb.
AndyAinscowFreelance programmer / Consultant

Commented:
>>So can you please tell me how to change the code.

You run whichever code is appropriate to the calling form.  eg. with a switch/case statement or an if statement.
Retired
Distinguished Expert 2017
Commented:
Hi RadhaKrishnaKiJaya;

Change your code as follows

'' Reference to the form that created this one
Dim formObj As Form

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

Now when you need to access the formObj from this form you need to do something like this

Select Case formObj.GetType().Name
    '' Enter the form name in the case statements, this app has forms Form1 and Form3
    Case "Form1"
        '' This writes to Form1's TextBox1
        Dim f1 = CType(theForm, Form1)
        f1.TextBox1.Text = "Hello World"
    Case "Form3"
        '' This writes to Form3's TextBox1
        Dim f3 = CType(theForm, Form3)
        f3.TextBox1.Text = "Hello World 3"
    Case Else
        Console.WriteLine("Form not found")
End Select

Open in new window

Author

Commented:
Thank you very much. It worked!!
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem RadhaKrishnaKiJaya, glad I was able to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial