Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

how do you right the 'get' and 'set' accessors for an array property

So I'm trying to use a instance variable that is an array. I want to write a property that will access this private instance variable. I can't find the syntax to do this.
0
john_schaffner
Asked:
john_schaffner
  • 2
1 Solution
 
Ravi SinghSenior Software EngineerCommented:
Hi, syntax is:

      public class MyClass {
               
            //Instance array variable
            private string[] dataArray;

            //Property
            public string[] Data {
                  get {
                        return this.dataArray;
                  }
                  set {
                        this.dataArray = value;
                  }
            }
      }
0
 
Ravi SinghSenior Software EngineerCommented:
If your defining a collection class then you could also make use of an indexer, which would allow users of your class to use the [] operator on a reference (just like using a standard array):

e.g.

MyClass myClass = new MyClass();
string someData = myClass[0];

See: http://www.developer.com/net/csharp/article.php/1547581

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now