?
Solved

arrayList question

Posted on 2006-04-18
9
Medium Priority
?
439 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:k3n51mm
  • 4
  • 2
7 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16479154
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16479228
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
 
LVL 1

Author Comment

by:k3n51mm
ID: 16479273
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16479398
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16479487
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
 
LVL 1

Author Comment

by:k3n51mm
ID: 16480859
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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16481772
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

839 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