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. Accessing properties as an array

Accessing properties as an array

Scheduled Pinned Locked Moved C#
questiondata-structures
8 Posts 5 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.
  • O Offline
    O Offline
    Omega501
    wrote on last edited by
    #1

    Is it possible to have a propety as an array? If so, how do I access it?? I usually create my properties in this manner

    class Object{
    private string sProperty;

    public string Property{
    get{return sProperty;}
    set{sProperty = value;}
    }

    Can I do the following, and how do I properly access the array

    class Object{
    private string[] sProperty; // Dimension it elsewhere

    public string[] Property{ // how do I access the array in the get/set methods?
    get{return sProperty;}
    set{sProperty = value;}
    }

    T O 2 Replies Last reply
    0
    • O Omega501

      Is it possible to have a propety as an array? If so, how do I access it?? I usually create my properties in this manner

      class Object{
      private string sProperty;

      public string Property{
      get{return sProperty;}
      set{sProperty = value;}
      }

      Can I do the following, and how do I properly access the array

      class Object{
      private string[] sProperty; // Dimension it elsewhere

      public string[] Property{ // how do I access the array in the get/set methods?
      get{return sProperty;}
      set{sProperty = value;}
      }

      T Offline
      T Offline
      thomasa
      wrote on last edited by
      #2

      I don't know about string[], but if you use ArrayList like: private ArrayList alBtnCommands = new ArrayList(); public ArrayList ButtonCommands { get { return alBtnCommands; } set { alBtnCommands = value; } } then you can call it by: currentButton.ButtonCommands.Add(AnItem); currentButton.ButtonCommands.RemoveAt(i); // i(integer) is an index SomeThing = currentButton.ButtonCommands[i]; currentButton.ButtonCommands[i] = SomeThing; It's not what you asked for, but it might be similar for string[] Hope it helps Thomas

      H 1 Reply Last reply
      0
      • O Omega501

        Is it possible to have a propety as an array? If so, how do I access it?? I usually create my properties in this manner

        class Object{
        private string sProperty;

        public string Property{
        get{return sProperty;}
        set{sProperty = value;}
        }

        Can I do the following, and how do I properly access the array

        class Object{
        private string[] sProperty; // Dimension it elsewhere

        public string[] Property{ // how do I access the array in the get/set methods?
        get{return sProperty;}
        set{sProperty = value;}
        }

        O Offline
        O Offline
        OmegaSupreme
        wrote on last edited by
        #3

        Do it just like you wrote : public string[] Property{ get{return sProperty;} set{sProperty = value;} } and access it like so Object o = new Object(); o.Property = new String[] {"foo", "bar"}; Console.WriteLine("{0} {1}", o.Property[0], o.Property[1]); another trick is to use default properties[^] write you property like this public string this[int index] { get { return sProperty[index]; } set { sProperty[index] = value; } } and access like this Object o = new Object(); o.Property = new String[2]; o[0] = "foo"; o[1] = "bar"; Console.WriteLine("{0} {1}", o[0], o[1]); HTH


        The smaller the mind the greater the conceit. Aesop

        J O 2 Replies Last reply
        0
        • O OmegaSupreme

          Do it just like you wrote : public string[] Property{ get{return sProperty;} set{sProperty = value;} } and access it like so Object o = new Object(); o.Property = new String[] {"foo", "bar"}; Console.WriteLine("{0} {1}", o.Property[0], o.Property[1]); another trick is to use default properties[^] write you property like this public string this[int index] { get { return sProperty[index]; } set { sProperty[index] = value; } } and access like this Object o = new Object(); o.Property = new String[2]; o[0] = "foo"; o[1] = "bar"; Console.WriteLine("{0} {1}", o[0], o[1]); HTH


          The smaller the mind the greater the conceit. Aesop

          J Offline
          J Offline
          Jonathan de Halleux
          wrote on last edited by
          #4

          The cop (FxCop) says do not return array as properties... Jonathan de Halleux.
          www.dotnetwiki.org

          O 1 Reply Last reply
          0
          • J Jonathan de Halleux

            The cop (FxCop) says do not return array as properties... Jonathan de Halleux.
            www.dotnetwiki.org

            O Offline
            O Offline
            OmegaSupreme
            wrote on last edited by
            #5

            Funny, the Cop didnt seem to have a problem with the little snippet I compiled :confused: But if they're the rules fair enough. Cheers for the info.


            The smaller the mind the greater the conceit. Aesop

            1 Reply Last reply
            0
            • T thomasa

              I don't know about string[], but if you use ArrayList like: private ArrayList alBtnCommands = new ArrayList(); public ArrayList ButtonCommands { get { return alBtnCommands; } set { alBtnCommands = value; } } then you can call it by: currentButton.ButtonCommands.Add(AnItem); currentButton.ButtonCommands.RemoveAt(i); // i(integer) is an index SomeThing = currentButton.ButtonCommands[i]; currentButton.ButtonCommands[i] = SomeThing; It's not what you asked for, but it might be similar for string[] Hope it helps Thomas

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Using an IList implementation like the ArrayList isn't always the answer. An array is fixed. Items can't be added or removed. This is often necessary in cases like returning the fonts installed on the system, or returning the printers installed on the system. In these cases, though, the property is read only (only implements the get accessor). If it was a list, developers can easily modify it (without even instantiating a new one) which might corrupt the behavior of the object.

              Microsoft MVP, Visual C# My Articles

              1 Reply Last reply
              0
              • O OmegaSupreme

                Do it just like you wrote : public string[] Property{ get{return sProperty;} set{sProperty = value;} } and access it like so Object o = new Object(); o.Property = new String[] {"foo", "bar"}; Console.WriteLine("{0} {1}", o.Property[0], o.Property[1]); another trick is to use default properties[^] write you property like this public string this[int index] { get { return sProperty[index]; } set { sProperty[index] = value; } } and access like this Object o = new Object(); o.Property = new String[2]; o[0] = "foo"; o[1] = "bar"; Console.WriteLine("{0} {1}", o[0], o[1]); HTH


                The smaller the mind the greater the conceit. Aesop

                O Offline
                O Offline
                Omega501
                wrote on last edited by
                #7

                Thanx - this is exactly what I needed. I only need it because I'm converting some VB code and it does it this way. What is the FxCop mentioned?

                O 1 Reply Last reply
                0
                • O Omega501

                  Thanx - this is exactly what I needed. I only need it because I'm converting some VB code and it does it this way. What is the FxCop mentioned?

                  O Offline
                  O Offline
                  OmegaSupreme
                  wrote on last edited by
                  #8

                  http://www.gotdotnet.com/team/fxcop/ [^] Check it out, it helps you write better code.


                  The smaller the mind the greater the conceit. Aesop

                  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