arrayList question

I want to declare an arrayList on the main form of my VB.Net 2005 MDI application that is able to be updated by child forms. So, I was planning on using this as a global declaration, prior to the Form1_Load sub:

    Friend myArrayList() As ArrayList

Then inside Form1, I have tried to instanciate in the following ways:    

    myArrayList() = New ArrayList   'throws error --> Number of indices is less than the number of dimensions of the indexed array.
    myArrayList = New ArrayList     'throws error --> Value of type System.Collections.ArrayList cannot be converted to 1-dimensional
                                                                           array of System.Collections.ArrayList


What I am trying to do is keep a list of opened related files which I call a filegroup. Certain application menu items are enabled when all files in a filegroup are open in the application. When one of the files in a filegroup is closed, I need to be able to disable the appropriate menu items, since filegroup functions should no longer be enabled.

Obviously I am missing something simple with this arrayList thing. Help?

Thanks
LVL 1
k3n51mmAsked:
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.

Fernando SotoRetiredCommented:
k3n51mm

    Friend myArrayList() As ArrayList
    Friend myArrayList2 As ArrayList

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load

        ' This defines an ArrayList of ArrayList
        myArrayList(5) = New ArrayList
        ' This Defines one ArrayList
        myArrayList2 = New ArrayList

    End Sub

Fernando
0
Fernando SotoRetiredCommented:
Hi k3n51mm;

From my above code this will define 6 ArrayList:

    myArrayList(5) = New ArrayList

Each of the following is a different ArrayList

    myArrayList(0)
    myArrayList(1)
    myArrayList(2)
    myArrayList(3)
    myArrayList(4)
    myArrayList(5)

Where this is just one ArrayList

    myArrayList2 = New ArrayList

I hope that this was of some help.

Fernando
0
k3n51mmAuthor Commented:
For chrissakes! Only 9 minutes have gone by and now I can't delete this question. I found the answer though, for anyone who comes looking.

I studied too much. All I had to do was Dim the stupid thing and use it. The global declaration
was fine. The difference was in Form1:

Dim myArrayList As New ArrayList
myArrayList.Add(arrayItem)
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Arthur_WoodCommented:
post a 0 point request to community support.  Ask that this question be PAQ'ed (since you answered it yourself) and they will refuund your points.

Include the URL of this question, in your request.

AW
0
Fernando SotoRetiredCommented:
Hi k3n51mm ;

If you did something like this stated in your question:

Public Class Form1

    Friend myArrayList() As ArrayList

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load

        Dim arrayItem As String = "Test"

        Dim myArrayList As New ArrayList
        myArrayList.Add(arrayItem)

    End Sub

End Class

Then the statement Dim myArrayList As New ArrayList is not referenceing the same variable myArrayList because the one inthe form load event hids the one that is global.

Fernando
0
k3n51mmAuthor Commented:
Fernando, thanks for the response. According to what I researched on this subject (way too much), I am instanciating the global declaration when I Dim the ArrayList inside Form1. This is why I can see it from Form2 (an MDI child form). If the Dim statement inside form1 actually overrode the global declaration somehow, then this would not be possible.
0
Fernando SotoRetiredCommented:
Hi k3n51mm;

Here is a sample project proving my point. Create a new project. Open the code for Form1 copy the code below marked Form1 and replace the code in the project Form1. Now create a Form2 and replace its code with Form2 code below. Then compile.

1. Run the program and when the MDI form comes up click on the menu button Create Child.
2. When the child displays click on its only button. You will get a message box stating that
    "Object reference not set to an instance of an object."
3. Now click on the menu button "Create Global myArrayList"
4. Now click on the child only button once again and you will get the message.
    "This Entry was created by clicking on the Main Menu in Parent MDI form"

Go through the code I commented it.

'=================================================================
' Form1 Code Start
'=================================================================
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.Text = "Create Child"
'
'MenuItem2
'
Me.MenuItem2.Index = 1
Me.MenuItem2.Text = "Create Global myArrayList"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(472, 422)
Me.IsMdiContainer = True
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"

    End Sub

#End Region

    Friend myArrayList As ArrayList

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

        ' The myArrayList declaration below, local scope, hides the global
        ' declaration above in Class/Global scope. This variable will not
        ' Exist outside of the Form_Load Subroutine therefore can not be
        ' accessed from outside this subroutine.
        Dim myArrayList As New ArrayList
        myArrayList.Add("Test String in Form Load")

    End Sub

    Private Sub MenuItem1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MenuItem1.Click

        ' Create The child form and display it
        Dim f2 As New Form2
        f2.MdiParent = Me
        f2.Show()

    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MenuItem2.Click

        ' This initializes the myArrayList in Class/Global scope with the
        ' below string.
        myArrayList = New ArrayList
        myArrayList.Add("This Entry was created by clicking" & _
            ControlChars.CrLf & " on the Main Menu in Parent MDI form")

    End Sub
End Class

'==========================================================
' Form1 Code End
'==========================================================


'==========================================================
' Form2 Code Start
'==========================================================

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(88, 80)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 270)
Me.Controls.Add(Me.Button1)
Me.Name = "m"
Me.Text = "Form2"
Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Get the reference to the MDI parent form
        Dim MDI As Form1 = CType(Me.MdiParent, Form1)

        ' This will print the message set into the myArrayList in the
        ' MDIParent form. If the code in the parents Form_Load event
        ' initialized the Global variable myArrayList then this Try
        ' block will not fail. But if the code in the Form_Load event
        ' did not initialize the Global variable myArrayList then this
        ' block of code will throw an error and the message will read
        ' "Object reference not set to an instance of an object."
        ' If you now click on the menu bar button marked
        ' "Create Global myArrayList" then the message box will read
        ' "This Entry was created by clicking on the Main Menu in Parent MDI form"
        Try
            MessageBox.Show("->" & MDI.myArrayList(0).ToString() & "<-")
        Catch es As Exception
            MessageBox.Show(es.Message)
        End Try

    End Sub
End Class

'=====================================================
' Form2 Code End
'=====================================================


Let me know if you have any questions.

Fernando
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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.