[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

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
0
IvanHowarth
Asked:
IvanHowarth
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now