none
how to use public property with string array mode? RRS feed

  • Question

  • Hello, I am trying to pass data from one form to another form, and since there are approximately 5 data from the form1, the best way is using array.

    'form2
    Public Property consult() As String

    But I came up with a problem and I do not know how to handle it

    'form2
    
    Dim a,b,c,d,e as String
    a = consult(0)
    b = consult(1)
    c = consult(2)
    d = consult(3)
    e = consult(4)

    in form1 is

    'form1
    Dim frm2 as form2()
    frm2.consult(0) = txt_a.text
    frm2.consult(1) = txt_b.text
    frm2.consult(2) = txt_c.text
    frm2.consult(3) = txt_d.text
    frm2.consult(4) = txt_e.text
    frm2.Show()
    Up to here I have been able to arrive and I know that it is wrong, how would I do it?

    Sunday, November 4, 2018 5:49 AM

All replies

  • You dont use a class property in form2:

          Public Property consult() As String

    just a public variable and you need to dimension it:

          Public consult(5) As String

    and I think you need a new form ie:

          Dim frm2 as New form2()

    Sunday, November 4, 2018 7:33 AM
  • You dont use a class property in form2:

          Public Property consult() As String

    just a public variable and you need to dimension it:

          Public consult(5) As String

    and I think you need a new form ie:

          Dim frm2 as New form2()

    Hi, It doesn't work

    Public consult(5) As String

    Is there any other way?

    Sunday, November 4, 2018 4:02 PM

  • Hi, It doesn't work

    Public consult(5) As String

    Is there any other way?

    Somehow I knew you would say that. :)

    There are many ways to do this btw. I am just working with what you have.

    What does not work? Do you get an error message? What line is it on? What code are you running?

    I showed 3 things. Did you put the array(5) in the second form?

    It seems to work like this image shows is that what you want?



    First form is form4 (they can be any name form). Here its important to use NEW so the second form is loaded and the array is declared, then we set the values of the array:

    'the first form
    Public Class Form4
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            'create the second form
            Dim fm As New Form3
    
            'defing a value for the array(0)
            fm.consult(0) = "test"
    
            'show the second form
            fm.Show()
    
        End Sub
    End Class

    and the second is fomr3:

    'the other second form
    Public Class Form3
    
        Public consult(5) As String
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Label1.Text = consult(0)
    
    
        End Sub
    End Class

    PS Feel free to join in anytime everybody! Corrections, other ways.



    Sunday, November 4, 2018 4:58 PM
  •  The best way to pass the data would all depend on when and how you need to pass the data,  and what the second form is doing with the data.

     For example,  is Form2 modifying the strings in the array property and then form1 is using the modified values from the property?  ... or maybe you only need to pass the string array to Form2 which uses and/or displays the strings, and form1 no longer uses the strings for anything after that?

     In some cases you may want to pass the array through the constructor of Form2 when you create the new instance of Form2 in Form1.  For example...

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim somedata() As String = {"a", "b", "c", "d", "e"} 'get an array of your strings
            Dim f2 As New Form2(somedata) 'pass the array to the constructor of Form2
            f2.Show()
        End Sub
    
    End Class


    Public Class Form2
    
        Public Sub New(data As String()) 'your own custom constructor for Form2 which requires a string array to be passed to it
            InitializeComponent() 'this is required, do not remove
            MyData = data 'set the MyData property to the string array passed to this constructor
        End Sub
    
        Private _MyData As String() = {}
        Public Property MyData As String()
            Get
                Return _MyData
            End Get
            Set(value As String())
                _MyData = value
                ListBox1.DataSource = _MyData
            End Set
        End Property
    
    End Class


     

     You can also just set the public property of Form2 when the New instance if it is created...

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 As New Form2 'create a New instance of Form2
            f2.MyData = {"a", "b", "c", "d", "e"} 'set the public string array property
            f2.Show()
        End Sub
    
    End Class


    Public Class Form2
    
        Private _MyData As String() = {}
        Public Property MyData As String()
            Get
                Return _MyData
            End Get
            Set(value As String())
                _MyData = value
                ListBox1.DataSource = _MyData
            End Set
        End Property
    
    End Class
     

     Both examples above produce this result...

     

     

     EDIT:  PS - The property that you declared is just a simple String type,  it is not declared as a String Array type....

    Public Property consult() As String 'should be... Public Property consult() As String()

     Also,  as Tom mentioned,  you should be creating a new instance of Form2.

        Dim frm2 As Form2
    
        'should be...
    
        Dim frm2 As New Form2


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

    • Edited by IronRazerz Sunday, November 4, 2018 9:49 PM
    Sunday, November 4, 2018 6:53 PM
  • works pretty well, I can use _MyData in another?, for example

    'frm2 -------------------------------
    Public Class frm2
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    Label1.Text = _MyData(0)
    Label2.Text = _MyData(1)
    Label3.Text = _MyData(2)
    End Sub
    
    End Class




    • Edited by Royer12 Sunday, November 4, 2018 8:58 PM add item
    Sunday, November 4, 2018 8:56 PM
  • works pretty well, I can use _MyData in another?, for example

    'frm2 -------------------------------
    Public Class frm2
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    Label1.Text = _MyData(0)
    Label2.Text = _MyData(1)
    Label3.Text = _MyData(2)
    End Sub
    
    End Class




    Hi,

    I think this is OK,

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 5, 2018 5:23 AM