|
-
Jun 22nd, 2009, 11:19 PM
#1
Thread Starter
PowerPoster
Pass form as paramater
Just ran into an issue with VB.net after years in VB6. I'm attempting to pass a form to a public sub without success.
This worked in vb6
vb Code:
Public Sub MySub(frm As Form)
MsgBox (Val(frm.Text1.Text) + Val(frm.Text2.Text))
End Function
But when I attempt to do the same in .net i get an error with the below saying Text1 and Text2 are not members of System.Windows.Forms.Form.
vb Code:
Public Sub MySub(ByRef frm As System.Windows.Forms.Form)
MessageBox.Show(Val(frm.Text1.Text) + Val(frm.Text2.Text))
End Sub
Any idea's?
-
Jun 22nd, 2009, 11:23 PM
#2
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
-
Jun 22nd, 2009, 11:27 PM
#3
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.
-
Jun 22nd, 2009, 11:48 PM
#4
Thread Starter
PowerPoster
Re: Pass form as paramater
As the Sub is used by up to 5 different forms I'll need to use the below method.
 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
-
Jun 22nd, 2009, 11:51 PM
#5
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.
-
Jun 23rd, 2009, 12:58 AM
#6
Thread Starter
PowerPoster
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
-
Jun 23rd, 2009, 01:01 AM
#7
New Member
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)?
-
Jun 23rd, 2009, 01:12 AM
#8
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()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|