Re: Pass form as paramater
Pass in the actual type of your form, which is its name.
So if your form is Form1, pass in
Otherwise if you want/need to pass in the object as the parent type of just "form" then you will need to cast it to the more specific type (again the forms name) inside your subroutine.
You also do not need to pass ByRef, forms are reference types and always passed by reference in .NET
Re: Pass form as paramater
Just note that what you are doing there would also work in VB.NET if you had Option Strict Off, which would allow late-binding. It's good that you have it On though. Late-binding is rarely actually required and tends to make people lazy when it comes to writing code, which is when errors creep in. Option Strict On forces you to think about the types you are using and makes it less likely that an error will be missed during development and crash the app at run time.
Re: Pass form as paramater
As the Sub is used by up to 5 different forms I'll need to use the below method.
Quote:
Originally Posted by
kleinma
Otherwise if you want/need to pass in the object as the parent type of just "form" then you will need to cast it to the more specific type (again the forms name) inside your subroutine.
Thanks :thumb:
Re: Pass form as paramater
If you're passing different types of forms then you shouldn't really be using the same method. You should only use the same method if you're using the same functionality but those different forms won't all have the same Text1 and Text2 members. Even if they all have members with those names they still won't be the same members so it's not the same functionality so you shouldn't be using the same method.
Re: Pass form as paramater
jmcilhinney, if 3 different forms all call the same sub (eg below), how could you pass the form?
kleinma, I thought the below would work but it doesn't? I get the same error messages. Any suggestions?
vb Code:
Public Sub DoSub(ByVal frm As System.Windows.Forms.Form)
Select Case frm.Name
Case "Form1"
frm = CType(frm, Form1)
Case "Form2"
frm = CType(frm, Form2)
End Select
frm.TextBox3.Text = Val(frm.TextBox1.Text) + Val(frm.TextBox2.Text)
End Sub
Re: Pass form as paramater
just a thought .. if different forms need to be passed to the same procedure, and each form have two text boxes are your example shows above, maybe you should consider creating an interface, and declaring your parameter as an interface instance, ie: byref x as IFormInterface (or whatever you want to call it)?
Re: Pass form as paramater
You shouldn't be passing the forms at all. You should just be passing the strings:
vb.net Code:
Public Function DoSub(ByVal str1 As String, ByVal str2 As String) As Double
Return Val(str1) + Val(str2)
End Function
In any form you can then call it like so:
vb.net Code:
Me.TextBox3.Text = DoSub(Me.textBox1.Text, Me.TextBox2.Text).ToString()