Assigning Object array elements to controls

I have created an object array containing different datatypes in one class (which works), now want to assign the elements
in another. Please can anyone correct what I have done wrong? Thanks in advance.
 
Public Class myForm1

    Public Function GetArray() As Array
        Dim A(2) As Object
        A(0) = MyString 'contains a string value
        A(1) = MyBoolean 'contains a boolean value
        Return A
    End Function

End Class
----------------------------------------------------
Public Class mySecondForm
    Dim MyArray(2) As Object

    Private Sub mySecondForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim frm As New myForm1
       
        MyArray(2) = frm.GetArray '????????????? NOT WORKING - is this the correct syntax?
        lblMyLable.Text = CType(MyArray(0), String)

    End Sub
End Class
IvanHowarthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omegaomegaDeveloperCommented:
Hello, Ivan,

You are putting the entire array created in "GetArray" into the element #2 of the array "MyArray".  You are then referencing element #0 of MyArray, which (unless there is other code somewhere that sets it) will be empty.

What is it that you actually want to do?

If you want MyArray to be the array provided by GetArray use:

        MyArray = frm.GetArray
        lblMyLable.Text = CType(MyArray(0), String)

If you want to access element #0 of the array stored in element #2 of MyArray use:

        MyArray(2) = frm.GetArray
        lblMyLable.Text = CType(MyArray(2)(0), String)

Cheers,
Randy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IvanHowarthAuthor Commented:
You're right! It is the first solution I'm trying to reach. However - using "MyArray = frm.GetArray" generates the following:

Option Strict On disallows implicit conversions from 'System.Array' to '1-dimensional array of System.Object'

0
omegaomegaDeveloperCommented:
Hello, Ivan,

The "simple" solution is to turn "Option Strict" off.  The better solution is to declare and convert your objects explicitly.  You have declared MyArray to be a one-dimensional Object array.  Try replacing the assignment line as shown:

        '''MyArray(2) = frm.GetArray
        MyArray = DirectCast(frm.GetArray, Object())

Cheers,
Randy

0
IvanHowarthAuthor Commented:
Randy - You're the Master!

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.