none
New Form costructor with byVal RRS feed

  • Question

  • HI

    Working with Winform 

    I want to pass FORM1  a string to a FORM2 in another Project of my solution.

    This is the code.

    Public Class Form1
    
    	Public Property Nome As String = ""
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    		Nome = "MioNome"
    		Dim f As New Proj2.Form2(Nome)
    		f.ShowDialog()
    	End Sub
    
    End Class
    
    
    
    Public Class Form2
    	Private Property Nomemio As String = ""
    	' New Form2 costructor of 
    	Public Sub New(ByVal ndb As String)
    		' La chiamata è richiesta dalla finestra di progettazione.
    		InitializeComponent()
    		' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
    		Nomemio = ndb
    	End Sub
    	Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
    		MessageBox.Show(Nomemio)
    	End Sub
    End Class
    

    I get this error :  Form2 is a Class Type and cannot be used as expression

    What i miss ?

    Wednesday, December 20, 2017 8:47 PM

Answers

  • See the following solution

    https://1drv.ms/u/s!AtGAgKKpqdWjiRMoZfMWTpTs2KO8


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Claudio111 Wednesday, December 20, 2017 11:59 PM
    Wednesday, December 20, 2017 10:14 PM
    Moderator
  • I want pass just a string not a form. I want just to lnow why it is necessary write the default constructor to avoid the error i get at the begonning of this discussion

    That makes a lot more sense - I was just going by what you showed.

    In the second form then, set the constructor up to accept a string. Test that the string "IsNotNullOrEmpty" then it's the same as before.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Wednesday, December 20, 2017 11:59 PM ...added link
    • Marked as answer by Claudio111 Thursday, December 21, 2017 12:06 AM
    Wednesday, December 20, 2017 11:58 PM

All replies

  • i forget to say.....I add reference to PROJ1 for Proj2
    Wednesday, December 20, 2017 8:47 PM
  • Claudio,

    You say "..in the same project". Do you mean in the same solution?

    When you look at Solution Explorer, do you see both projects?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, December 20, 2017 8:50 PM
  • See the following solution

    https://1drv.ms/u/s!AtGAgKKpqdWjiRMoZfMWTpTs2KO8


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Claudio111 Wednesday, December 20, 2017 11:59 PM
    Wednesday, December 20, 2017 10:14 PM
    Moderator
  • Hi Karen

    I Have Form 1 in project 1 and Form2 in Project 2 both in the same Solution

    Wednesday, December 20, 2017 10:19 PM
  • Hi Karen

    I Have Form 1 in project 1 and Form2 in Project 2 both in the same Solution

    Obviously I'm not Karen, but if it's in the same solution then just fully qualify it so that the recipient knows where to get the instance.

    Also, be sure not to use the default instance which is what you showed above. It will come back to bite you. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, December 20, 2017 10:24 PM
  • Also, be sure not to use the default instance which is what you showed above....

     OP is "trying" creating a new instance of the form.  8)

    		Dim f As New Proj2.Form2(Nome)
    		f.ShowDialog()


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, December 20, 2017 10:48 PM
    Wednesday, December 20, 2017 10:47 PM
  • Also, be sure not to use the default instance which is what you showed above....

     OP is "trying" creating a new instance of the form.  8)

    		Dim f As New Proj2.Form2(Nome)
    		f.ShowDialog()


    If you say it can`t be done then i`ll try it


    He's not disposing it, but you're right -- mea culpa.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, December 20, 2017 10:55 PM
  • He's not disposing it, but you're right -- mea culpa.

    "A problem well stated is a problem half solved.” - Charles F. Kettering


     True.  That should be done with a dialog form.  8)

    If you say it can`t be done then i`ll try it

    Wednesday, December 20, 2017 10:59 PM
  • The code i wrote was right

    The solution is to add to Form2 in Project2 the default constructor

    Public Sub New()

    InitializeComponent()
    End Sub

    This eliminate the error.

    It is ok now even if I don't understand why to add the default Constructor

    Wednesday, December 20, 2017 11:02 PM
  • Thank you again Karen
    Wednesday, December 20, 2017 11:02 PM
  • The code i wrote was right

    The solution is to add to Form2 in Project2 the default constructor

    Public Sub New()

    InitializeComponent()
    End Sub

    This eliminate the error.

    It is ok now even if I don't understand why to add the default Constructor

    You can create the other form using a parameterized constructor and in that New have it throw an exception if the minimum isn't met.

    Doing it that way, hand that value to a private field and in the other form's code, do whatever you had in mind all along.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Wednesday, December 20, 2017 11:06 PM ...typo
    Wednesday, December 20, 2017 11:05 PM
  • HI Frank

    can you explain in other words , I did not understand why to add the default (minimum) constructor

    Wednesday, December 20, 2017 11:14 PM
  • Hi Karen

    I Have Form 1 in project 1 and Form2 in Project 2 both in the same Solution

    That is what I have in my demo solution (there is a form1 in project two but never used).

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, December 20, 2017 11:17 PM
    Moderator
  • HI Frank

    can you explain in other words , I did not understand why to add the default (minimum) constructor

    If you have (in the same solution) Form1 with a button named "Button1" then the following will pass an instance of Form1 to a new instance of Form2:

    Public Class Form1
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
            ' Initialization here if applicable
    
        End Sub
    
        Private Sub _
            Button1_Click(sender As System.Object, _
                          e As System.EventArgs) _
                          Handles Button1.Click
    
            Using f2 As New Form2(Me)
                f2.ShowDialog()
    
                ' Let's talk some more after
                ' this..
            End Using
    
        End Sub
    End Class

    Notice that I passed "Me" in the constructor. Here's why - in the code for Form2:

    Public Class Form2
        Private _callingForm As Form
    
        Public Sub New(ByVal frm As Form)
            InitializeComponent()
    
            If frm Is Nothing Then
                Close()
            Else
                _callingForm = frm
            End If
        End Sub
    
    
    
        Private Sub _
            Form2_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
            ' Now the private field (variable) named 
            ' "_callingform" is the form that sent it
            ' here. I assume that's what you want to
            ' do?
    
        End Sub
    End Class

    Make sense?

    If not then let's talk some.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, December 20, 2017 11:26 PM
  • I want pass just a string not a form. I want just to lnow why it is necessary write the default constructor to avoid the error i get at the begonning of this discussion
    Wednesday, December 20, 2017 11:55 PM
  • I want pass just a string not a form. I want just to lnow why it is necessary write the default constructor to avoid the error i get at the begonning of this discussion

    That makes a lot more sense - I was just going by what you showed.

    In the second form then, set the constructor up to accept a string. Test that the string "IsNotNullOrEmpty" then it's the same as before.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Wednesday, December 20, 2017 11:59 PM ...added link
    • Marked as answer by Claudio111 Thursday, December 21, 2017 12:06 AM
    Wednesday, December 20, 2017 11:58 PM
  • Read well the discussion Frank. What i'm sayong is that i have to write both the default constructor i.e. Public sub new ....... End sub and the new constructor with parameter i.e. Public sub new(byval parname as string) . Without the defaullt constructor the string ithe compiler shows error.
    Thursday, December 21, 2017 12:05 AM
  • Read well the discussion Frank. What i'm sayong is that i have to write both the default constructor i.e. Public sub new ....... End sub and the new constructor with parameter i.e. Public sub new(byval parname as string) . Without the defaullt constructor the string ithe compiler shows error.

    If you want, let's explore this more tomorrow.

    You can pass anything in (literally anything). If it gets involved, create a class and pass an instance of the class in.

    In the other form, you'll then have whatever you passed in. In that other form, you can also pass back out anything you want. It's all a matter of how you set it up.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, December 21, 2017 12:12 AM
  • Read well the discussion Frank. What i'm sayong is that i have to write both the default constructor i.e. Public sub new ....... End sub and the new constructor with parameter i.e. Public sub new(byval parname as string) . Without the defaullt constructor the string ithe compiler shows error.

     Did you try setting the string parameter in the constructor as "Optional"?  Then you can use just one constructor for the form.  For example...

    Form2 code....

    Public Class Form2
        Private TheString As String = ""
    
        Public Sub New(Optional SomeString As String = "")
            InitializeComponent()
    
            If SomeString.Trim <> "" Then
                TheString = SomeString
            Else
                TheString = "Some Other String"
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MessageBox.Show(TheString)
        End Sub
    End Class
     

     Then you can pass a String if you want,  or it can be called without passing anything...

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 As New Form2
            f2.Show()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim f2 As New Form2("The String I Passed")
            f2.Show()
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, December 21, 2017 12:42 AM
    Thursday, December 21, 2017 12:40 AM
  • @IronRazerz,

    Good idea if you never use Form2 as the main form as the project will not compile. It's prudent to separate the optional stuff into it's own constructor.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 21, 2017 12:57 AM
    Moderator
  • Karen,

     Yes that is true.  I was just taking it for granted that being it was named Form2,  it was not a startup form but,  it could be.  8)


    If you say it can`t be done then i`ll try it

    Thursday, December 21, 2017 1:24 AM
  • Hi Karen, Iron, Frank

    result of this discussion for me is the following

    If in Proj2  you have Form2 that is also the MAIN FORM for Proj2 and Form2 has only a Parametrized Constructor you get a Compiler error. So you have to include in Form2 also a constructor without Parameter.

    If in Proj2 you have another form, it say Form3 that is the Main form for Proj2, so in Form2 you can have a parametrized constructor and you don not need to include the constructor without parameter and you don't get the compiler error. 

    Thanks to all, to Karen for initial solution, to Iron for the word MAIN that opend my mind and to Frank for the suggestion to pass a form object  to another form that could be usefull in the future (not now)

    Have a good day

    Claudio

    Thursday, December 21, 2017 7:22 AM