473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 9069
The only good way to save data between pages is in a Session Object.

You would want to specify something like this:

Session["StrArray"] = MyStringArray;

and then use it elsewhere.

Lowell


James N wrote:
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Hello

Make the array static
internal static string[] array = new string[] { "one", "two", "three"};

The other poster mentioned the session object which is useful when the array
differs from a user to another, but if you are using the same array for all
users, that never changes, static is your best choice.

Best regards,
Sherif
"James N" <em************@yahoo.com> wrote in message
news:uY**************@TK2MSFTNGP10.phx.gbl...
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
You could make the array static, which would get the job done, but
leave the array open to arbitrary modification by your code.

Or, you could create a Singleton object that contains data needed by
various parts of your program, and store the data there under the
protection of your SIngleton class. You can read up on the Singleton
design pattern here:
http://www.c-sharpcorner.com/Code/20...tonPattern.asp

Nov 16 '05 #4
Bruce Wood <br*******@canada.com> wrote:
You could make the array static, which would get the job done, but
leave the array open to arbitrary modification by your code.

Or, you could create a Singleton object that contains data needed by
various parts of your program, and store the data there under the
protection of your SIngleton class. You can read up on the Singleton
design pattern here:
http://www.c-sharpcorner.com/Code/20...tonPattern.asp


Or you could read up on a thread-safe version...

http://www.pobox.com/~skeet/csharp/singleton.html

(I'm not sure why you'd want to know the "number of current
references" to the singleton, seeing as nothing decreases that value.
It seems a silly addition to me.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
1
by: James N | last post by:
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...
22
by: fd123456 | last post by:
Hi Tom ! Sorry about the messy quoting, Google is playing tricks on me at the moment. > Global.asax is where you normally have the Global Application > and Session variables and code to...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
by: Spiros Bousbouras | last post by:
Let's say I have a global variable int var which I want to be known to translation units T1 and T2, I want T1 to be able to read its value and modify it and T2 to be able to read its value but not...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
by: sunil | last post by:
Hi, Am developing one shared library for one application. In that .so am having one global array of structure ( two more different structure pointers in this struct). Once the application is...
3
by: ankugoe7 | last post by:
Is it possible to share global variables such that all applications which use shared libraries can see the changes made to the global variables by the other applications.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.