VB.NET - How to substitute frame name with the string variable?


I pass two variables to the procedure. One is output to be printed in the form's textbox and another is a form's Name. How do I actually substitute Form1 with the frmName to make sure that the text is printed in the textbox of the form that I need?

I can implement it using Select Case or if.. then basing on frmName value, but I wonder how to do that avoiding it.

Public Sub Result(ByVal output As String, ByVal frmName As String)
        With Form1.txtServerStatus
            .Text &= vbCrLf & output
        End With
    End Sub

Open in new window

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.

Dont pass the form name as a string.  Pass the form as an object of the type of form it is.  
Public Sub Result(ByVal output As String, ByVal frmIN As MYFORMTYPE)
Of course if you have different Form Classes you are trying to pass to this procedure you may need to subclass all of the forms to a single class so that they include a text box called txtServerStatus.

ZaurbAuthor Commented:

I have 5 forms with the similar content and similar design, but not exactly the same functionality.
Depending on which specific form is used for working, I pass a form name variable to a class, that does the rest of the processing. txtServerStatus is available on all forms and prints the information on steps carried out in the class.
Since there're several users working contemporaneously they will see different information in the txtServerStatus, depending on operations they make.
For this reason, I pass the form name to a class, so that after procedures execute, they will print the result to a specific form.

As for this:
Public Sub Result(ByVal output As String, ByVal frmIN As MYFORMTYPE)

How should I implement it in my case?

You have 5 forms that are very similar with slight variations in functionality.  You would want to make a base form that contains the common objects and functionallity.  Then make the 5 specific forms by INHERITING from the base form.  This will allow the 5 specific forms to have exactly the same COMMON objects and functions and still give you the ability to modify them for their individualized functionality.
To make a base form, simply add a new form to your project.  Design it with any common components and call it  frmBASE.   For example, on frmBase put a textbox called txtServerStatus in the appropriate place to be used for the 5 forms you need.
To create your 5 specific forms, right click on  your project and select ADD INHERITED FORM. Name the new form and pick frmBASE from the selection list.  A new form exactly like frmBase will be added to your design editor.
Individualize each of the 5 forms for their unique functionallity.  Then you can create functions like the one below which allows a FORM of type frmBASE as a parameter.  Within the function, all members and properties of frmBASE are accessible.
Create Public Sub Result(ByVal Output as String, ByVal frmIn as frmBASE)
      frmIn.txtServerStatus will be accessible
From any of the five forms you can call Result(mystring,ME)
Hope this helps

I thought about this problem some more.   You could use a case statement instead of inheritance.  For example
Public Sub Result(ByVal output As String, ByVal frmName As String)
    Select Case frmName
       Case "FORM1"
                    frm1.txtServerStatus.Text &= vbCrLF & output
      Case "FORM2"

                    frm2.txtServerStatus.Text &= vbCrLF & output
End Select
End Sub  
Of course to do this, frm1 and frm2 would have to be global variable names.   You could declare
     Public frm1 as FORM1 = new FORM1()
I have done workarounds like this before as opposed to recreating all of the forms as inheritted from a base form.
Good Luck

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
ZaurbAuthor Commented:
This is actually how I do it now. And it seems to be the less complicated way to deal with this problem.
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.