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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. array property

array property

Scheduled Pinned Locked Moved C#
data-structuresquestion
7 Posts 6 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.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    I would like to define a property that holds 3 chars, so Im thinking of an array. Is this the way to do it? Can it be done with an automatic property somehow?

    public abstract class Package
    {
        private char\[\] request;
    
        public Package() 
        {
            request = new char\[3\];
        }
    
        public char\[\] Request
        {
            get { return request; }
            set { request = value; }
        }
    
    }
    
    T P OriginalGriffO L B 6 Replies Last reply
    0
    • T TMattC

      I would like to define a property that holds 3 chars, so Im thinking of an array. Is this the way to do it? Can it be done with an automatic property somehow?

      public abstract class Package
      {
          private char\[\] request;
      
          public Package() 
          {
              request = new char\[3\];
          }
      
          public char\[\] Request
          {
              get { return request; }
              set { request = value; }
          }
      
      }
      
      T Offline
      T Offline
      Thomas Daniels
      wrote on last edited by
      #2

      There are some ways you can do. What to choose, depends on your application, but most likely, way 1 and 2 will be most helpful to you.

      • Use an array, as you did. If you do this, check for the length when the array is changed:

        set
        {
        if (value.Length == 3)
        {
        request = value;
        }
        else
        {
        throw new Exception("Length of Request must be 3.");
        }
        }

      • Use a Tuple: http://msdn.microsoft.com/en-us/library/system.tuple%28v=vs.110%29.aspx[^]

        private Tuple request;

        public Package()
        {
        request = new Tuple('\0', '\0', '\0');
        }

        public Tuple Request
        {
        get { return request; }
        set { request = value; }
        }

        Items in the Tuple can be accessed like Request.Item1, Request.Item2, Request.Item3.

      • As properties in your class. Personally, I wouldn't use this unless it is really necessary for some reason.

        private char _char1;
        private char _char2;
        private char _char3;

        public Package()
        {
        _char1 = _char2 = _char3 = '\0';
        }

        public char Char1
        {
        get { return _char1; }
        set { _char1 = value; }
        }
        // and so on

      The quick red ProgramFOX jumps right over the Lazy<Dog>.

      1 Reply Last reply
      0
      • T TMattC

        I would like to define a property that holds 3 chars, so Im thinking of an array. Is this the way to do it? Can it be done with an automatic property somehow?

        public abstract class Package
        {
            private char\[\] request;
        
            public Package() 
            {
                request = new char\[3\];
            }
        
            public char\[\] Request
            {
                get { return request; }
                set { request = value; }
            }
        
        }
        
        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Arrays are so last century. Better options would be a string, a Tuple, a custom class or struct.

        1 Reply Last reply
        0
        • T TMattC

          I would like to define a property that holds 3 chars, so Im thinking of an array. Is this the way to do it? Can it be done with an automatic property somehow?

          public abstract class Package
          {
              private char\[\] request;
          
              public Package() 
              {
                  request = new char\[3\];
              }
          
              public char\[\] Request
              {
                  get { return request; }
                  set { request = value; }
              }
          
          }
          
          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          As Piebald suggests, a struct would be a lot better than an array: an array is a reference type, so it both takes more space and is slower to use than a struct - which is a value type, so it is "inline data" rather than a separate heap allocation and a reference field. And you can name the struct fields, which should make your code more readable as well.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • T TMattC

            I would like to define a property that holds 3 chars, so Im thinking of an array. Is this the way to do it? Can it be done with an automatic property somehow?

            public abstract class Package
            {
                private char\[\] request;
            
                public Package() 
                {
                    request = new char\[3\];
                }
            
                public char\[\] Request
                {
                    get { return request; }
                    set { request = value; }
                }
            
            }
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            That code has the interesting issue that the returned array can easily, even accidentally, be used to mess up the state of the Package. That's not necessarily the Worst Thing Ever (it is always possible to mess up objects with reflection anyway), but it's something to at least be aware of.

            1 Reply Last reply
            0
            • T TMattC

              I would like to define a property that holds 3 chars, so Im thinking of an array. Is this the way to do it? Can it be done with an automatic property somehow?

              public abstract class Package
              {
                  private char\[\] request;
              
                  public Package() 
                  {
                      request = new char\[3\];
                  }
              
                  public char\[\] Request
                  {
                      get { return request; }
                      set { request = value; }
                  }
              
              }
              
              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              I think the "best practice" ... the implementation details ... will be an "organic" function of the way you intend to use this data-structure; including what you want your code to communicate to yourself, and, possibly, others, in the future. One suggestion: before you draw any simple one-size-fits-all conclusions about how .NET allocates memory for Structs, and other "Value Types" compared to "Reference Types," please read Eric Lippert's two articles on this topic: [^], [^]. Eric is a gurus'-guru who was a key player in creating .NET for many years, and I think these articles provide a very valuable insight into what actually goes on in memory in .NET ... perhaps not it's not as clear-cut as many people think. There are many ways you could implement this, from simple to fancy, from one-shot single-purpose to general-use multi-purpose; a few years ago I wrote a multi-purpose class for maintaining a set of selected Char that was, perhaps, too fancy: I just looked at the code tonight and wondered why I made it so elaborate ... but I have a "thang" for writing multi-purpose :) If you care to say more about your goals here, I'll respond, and I am sure others will.

              «A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push»  Wittgenstein

              1 Reply Last reply
              0
              • T TMattC

                I would like to define a property that holds 3 chars, so Im thinking of an array. Is this the way to do it? Can it be done with an automatic property somehow?

                public abstract class Package
                {
                    private char\[\] request;
                
                    public Package() 
                    {
                        request = new char\[3\];
                    }
                
                    public char\[\] Request
                    {
                        get { return request; }
                        set { request = value; }
                    }
                
                }
                
                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                TMattC wrote:

                Can it be done with an automatic property

                One further comment: an automatic property is one where the private backing-field is created for you by using the declarative syntax: private char[] CharProp { set; get; } The moment you specify a "body" for either the 'set or the 'get, it's no longer an automatic property, and, since it's clear your use-case would involved accessing characters within the Array here, you'd have to do that in the 'get, and, for that to be done, you'd have to have an value to use as an Index into the Array, and there's no way to "pass in" a value to be used internally in a 'get, or 'set. The next version of C#, C#6, is going to add some features to Property declaration: [^]. Of course, with a Property whose Type is Array you can always use the 'SetValue and 'GetValue methods of the Array object; however, as you'll see in this example, 'GetValue returns a Type Object which will need to be cast to 'Char to be used in the way you probably want to use it.

                private char[] CharProp { set; get; }

                private void TestCharProp()
                {
                CharProp = new char[3] {'a', 'b', 'c'};

                char value2 = (char) CharProp.GetValue(2);
                
                CharProp.SetValue('x',2);
                

                }

                That's not the type of code I'd write: no error check for null; no check for bounds; unnecessary cast required. But, you can write your own methods to 'set and 'get, of course that do validate and do avoid the cast.

                «A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push»  Wittgenstein

                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