Type scope confusion.

  • Thread starter Thread starter garyusenet
  • Start date Start date
G

garyusenet

Hello everyone.

I have a project at the moment which is working fine. I originally
coded my code in form1.cs, as this was the default code file generated
by Visual Studio. However now i'm trying to organise it better, so have
renamed the file to ChooseWindow and chose the autorename feauture, and
everything works fine.

I have just added a second form which will become my primary form
eventually, and I will call the original form (ChooseWindow) from this
new primary form. The problem i have is that the array list is setup in
the original form and I cant access it in the new form I have just
created. I need to be able to access this arraylist in both code files.


How do i declare it so that I can access it in both code files and not
just the original ChooseWindow code file?

Thanks very much,

Gary.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHDocVw;

namespace InternetExplorerInterface
{
public partial class Choosewindow : Form
{
public Choosewindow() // Pull up the form
{
InitializeComponent();
}

ArrayList ar = new ArrayList();

}
 
Thanks very much, i'll look into what you suggested right away.

The array list is used to store all open internet explorer window
objects. This in turn is used to populate a listview. However I only
need to give the user an option of selecting which window to work with
if there is more than one explorer window open. So, I have created
another form and will only call this choosewindow form if their is more
than one internet explorer window.

In order to check how many explorer window objects are in the
arraylist, i need to be able to check it's size from the new form i've
just created.

Now i dont know if i should build the array in my new form, and then
access it in the original choosewindow form, or if i should keep it as
it is in the choosewindow form, and just change it's access so i can
access it in my new form.

Hope that explains it a bit better,

Thanks,
Gary.
 
I suspect that at some point you will want to populate that arraylist and
use its contents :=) A better approach would be to create a separate class
that exposes a public method which populates the arraylist. Then you can
create an instance of that class and call the arraylist method from whatever
form you wish.

public YourClass()
{
}

public ArrayList GetWhatever()
{
ArrayList al = new ArrayList();
try
{
... populate arraylist
}
catch (Exception ex)
{
throw ex;
}
return al;
}
 
Thanks very much, i'll look into what you suggested right away.

The array list is used to store all open internet explorer window
objects. This in turn is used to populate a listview. However I only
need to give the user an option of selecting which window to work with
if there is more than one explorer window open. So, I have created
another form and will only call this choosewindow form if their is more
than one internet explorer window.

In order to check how many explorer window objects are in the
arraylist, i need to be able to check it's size from the new form i've
just created.

Now i dont know if i should build the array in my new form, and then
access it in the original choosewindow form, or if i should keep it as
it is in the choosewindow form, and just change it's access so i can
access it in my new form.

It sounds to me as though the new form, Form2, will be called from the
original form. Is that true? Or is it the other way around? I'm a
little foggy on how the forms are supposed to be related.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top