Assigning Object array elements to controls

Posted on 2006-04-25
Last Modified: 2010-04-23
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
Question by:IvanHowarth
    LVL 12

    Accepted Solution

    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)


    Author Comment

    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'

    LVL 12

    Expert Comment

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



    Author Comment

    Randy - You're the Master!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now