Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Declaring 'Lists'

Declaring 'Lists'

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nlowdon
    wrote on last edited by
    #1

    Evening all ! Can anyone tell how i declare a list in my class so all the methods can see/access it or is this not possible ? I've created a list in button code in a form -

    List<Animal> zooAnimals = new List<Animal>();

    but can't seem to figure out how to make it global so other methods can access it. I tried

    public List <zooAnimals>;

    but that doesn't work Thanks in advance

    H W 2 Replies Last reply
    0
    • N nlowdon

      Evening all ! Can anyone tell how i declare a list in my class so all the methods can see/access it or is this not possible ? I've created a list in button code in a form -

      List<Animal> zooAnimals = new List<Animal>();

      but can't seem to figure out how to make it global so other methods can access it. I tried

      public List <zooAnimals>;

      but that doesn't work Thanks in advance

      H Offline
      H Offline
      humayunlalzad
      wrote on last edited by
      #2

      use the word static before the declaration. But then the list will belong to the class and not the object

      1 Reply Last reply
      0
      • N nlowdon

        Evening all ! Can anyone tell how i declare a list in my class so all the methods can see/access it or is this not possible ? I've created a list in button code in a form -

        List<Animal> zooAnimals = new List<Animal>();

        but can't seem to figure out how to make it global so other methods can access it. I tried

        public List <zooAnimals>;

        but that doesn't work Thanks in advance

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        Declare a class (public, static if you want) and define the list in that class. For example:

        public static class MyList {
        private static readonly List <zooAnimals> = new List<Animal>();;
        public static List<Animal> ZooAnimals {
        get {
        return zooAnimals ;
        }
        }
        }

        The need to optimize rises from a bad design. My articles[^]

        E 1 Reply Last reply
        0
        • W Wendelius

          Declare a class (public, static if you want) and define the list in that class. For example:

          public static class MyList {
          private static readonly List <zooAnimals> = new List<Animal>();;
          public static List<Animal> ZooAnimals {
          get {
          return zooAnimals ;
          }
          }
          }

          The need to optimize rises from a bad design. My articles[^]

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          readonly is not recommended for objects of this sort since it implies the objects of the list are unchangeable when in fact they are. It is a constantly source of errors for novices.

          Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
          Most of this sig is for Google, not ego.

          W 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            readonly is not recommended for objects of this sort since it implies the objects of the list are unchangeable when in fact they are. It is a constantly source of errors for novices.

            Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
            Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
            Most of this sig is for Google, not ego.

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            When posting the answer I though for awhile if I should declare the list readonly or not. When used correctly, readonly eliminates certain problems that can occur if the actual list is changed. I would say that it depends if you want the list to be changed or just to allow to modify it's content. That's why I also defined only getter for the property. The other thing was that the original question was, how to define a list globally so that others can see and use it. Based on the question my interpretation was (maybe incorrectly) that the list should not be changeable, but it's contents can still be modified.

            The need to optimize rises from a bad design. My articles[^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups