How do I create a global array that can be shared in my program?

J

James N

Here's the situation: I have 3 webforms in my project. Form 1 allows
the user to select a type of report to generate. There are 2 possible
type of reports, and therefore, Forms 2 and 3 are these two generated
reports. When being generated, both reports will need to use the exact
same data. The data are static strings and I plan to declare a global
array in Form 1, so that Form 2 and 3 can share them. In other words, I
don't want to be declaring identical arrays in both Form 2 and 3 because
that's just gonna waste space and make the code more messy. So the idea
is simple, but I, having problems creating/declaring a global array in
Form 1. Here's my approach:

//Form1.cs
namespace Report
{
public class Form1
{
internal string[] array = new string[] { "one", "two", "three"};
}
}

//Form2.cs
namespace Report
{
public class Form2
{
textBox.Text = array[0];
}
}

//Form3.cs
namespace Report
{
public class Form3
{
textBox.Text = array[1];
}
}

I got errors when compiling which says that "the type or namespace
'array' could not be found (are you missing a using directive or an
assembly reference?)". Why is it not working? I thought that the
"internal" modifier allows the variable to be accessible throughout the
assembly, so why would I need to include any directives or references?
 
M

Mohamoss

Hi James
It very normal you can't access it . even if you declared it as public , it
is still a member of class Form1 , and you (( can't access it unless throw
an object of type form1 " or thow the name of the class as you are
declaring it as Static" as the base of object oriented programming))
So , try it this way
namespace Report
{
public class Form1
{
internal string[] array = new string[] { "one", "two", "three"};
}
}

//Form2.cs
namespace Report
{
public class Form2
{
textBox.Text = Form1.array[0];
}
}

//Form3.cs
namespace Report
{
public class Form3
{
textBox.Text = Form1.array[1];
}
}
Hope this helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 

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

Top