Results 1 to 8 of 8

Thread: Pass form as paramater

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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:
    1. Public Sub MySub(frm As Form)
    2.  
    3. MsgBox (Val(frm.Text1.Text) + Val(frm.Text2.Text))
    4.  
    5. 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:
    1. Public Sub MySub(ByRef frm As System.Windows.Forms.Form)
    2.        
    3.         MessageBox.Show(Val(frm.Text1.Text) + Val(frm.Text2.Text))
    4.        
    5.     End Sub


    Any idea's?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

    Code:
    frm as Form1
    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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 View Post
    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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:
    1. Public Sub DoSub(ByVal frm As System.Windows.Forms.Form)
    2.  
    3.         Select Case frm.Name
    4.  
    5.             Case "Form1"
    6.                 frm = CType(frm, Form1)
    7.             Case "Form2"
    8.                 frm = CType(frm, Form2)
    9.         End Select
    10.  
    11.         frm.TextBox3.Text = Val(frm.TextBox1.Text) + Val(frm.TextBox2.Text)
    12.     End Sub

  7. #7
    New Member
    Join Date
    May 2009
    Posts
    4

    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)?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Pass form as paramater

    You shouldn't be passing the forms at all. You should just be passing the strings:
    vb.net Code:
    1. Public Function DoSub(ByVal str1 As String, ByVal str2 As String) As Double
    2.     Return Val(str1) + Val(str2)
    3. End Function
    In any form you can then call it like so:
    vb.net Code:
    1. Me.TextBox3.Text = DoSub(Me.textBox1.Text, Me.TextBox2.Text).ToString()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width