We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

john_schaffner
on
Medium Priority
210 Views
Last Modified: 2012-05-05
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.
Comment
Watch Question

Senior Software Engineer
Commented:
Hi, syntax is:

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

            //Property
            public string[] Data {
                  get {
                        return this.dataArray;
                  }
                  set {
                        this.dataArray = value;
                  }
            }
      }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Ravi SinghSenior Software Engineer

Commented:
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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.