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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James N

    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!
  • Lowell Heddings

    #2
    Re: How do I create a global array that can be shared in my program?

    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:[color=blue]
    > 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![/color]

    Comment

    • Sherif ElMetainy

      #3
      Re: How do I create a global array that can be shared in my program?

      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" <email_this_guy @yahoo.com> wrote in message
      news:uYVS33v3EH A.1204@TK2MSFTN GP10.phx.gbl...[color=blue]
      > 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![/color]


      Comment

      • Bruce Wood

        #4
        Re: How do I create a global array that can be shared in my program?

        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:


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: How do I create a global array that can be shared in my program?

          Bruce Wood <brucewood@cana da.com> wrote:[color=blue]
          > 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[/color]

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

          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


          (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 - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...