Re: Is it possible to have parameters in a property?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Nov 11, 2:54 pm, Stu <stumor...@xxxxxxxxx> wrote:
On Nov 11, 2:47 pm, "Alberto Poblacion" <earthling-



quitaestoparacontes...@xxxxxxxxxxxxx> wrote:
"Stu" <stumor...@xxxxxxxxx> wrote in message

news:37a0f339-e99b-44a1-aae5-bf337229601f@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I have a private array in a class, and I was just wondering if it's
possible to expose its elements with "array-like" syntax through a
property without exposing the full array, instead of using a method.
So for instance could I do something like this:

public class MyClass
{
 private int[] _NumberArray = new int[100];
 public int NumberArray[int index]
 {
    get
    {
         return _NumberArray[index];
    }
    set
    {
         _NumberArray[index] = value;
    }
 }

 public MyClass(){}
}

Then call it like this so that when MyClass.NumberArray[index] gets
assigned a value, it does it through the property:
MyClass myClass = new MyClass();
myClass.NumberArray[index] = 5;

I know this is easily accomplished through a method, but then I have
to pass the index and value as parameters rather than using array-like
syntax.  Not a big deal, more a matter of curiosity.

   No, the properties do not accept parameters, byt you can achieve a
similar efect through a class indexer. Just rename your NumberArray property
to "this", and then you can call it like this:

MyClass myClass = new MyClass();
myClass[index] = 5;

Thanks Alberto.  What you suggested is not the functionality I'm
looking for, since I've got multiple arrays in my class that I want to
index like that.  However it's still useful information to know.

Wow Peter, thanks for the very thorough explanation! Basically, the
functionality I'm looking for is to be able to expose the array
through a property but also to know when any of the values within the
array have changed. The "set" only fires when I assign the entire
array property a new value, not when any of the values within it
change. I think I can accomplish the functionality I'm looking for
using one of your examples above. Thanks again.
.



Relevant Pages

  • Re: Another spinoza challenge
    ...   byref; ... point with an array rather than a hypothetical struct syntax: ... Even when hiding the pointer away with array notation, ...
    (comp.lang.c)
  • Re: "Sorting" assignment
    ... If the array is already sorted, this means that you end up ... less time than a bubble sort, ...     int intLeft, ... I don't declare anything inside of a loop is why. ...
    (comp.programming)
  • Re: #defining an array within another in C language
    ... you do not know the number of elements of the array. ...     int var; ... C99 flexible array members or the plain old "struct hack". ...
    (comp.lang.c)
  • Re: Generic programming in C
    ... struct tag$ArrayInterface; ...     unsigned int Flags; ... specific type as the contents of the array. ...
    (comp.lang.c)
  • Re: Allocattable and namelist object
    ...   NAMELIST/TOTO/TAB ... array can be in a namelist object. ... The shape of an allocatable array can ... A deferred-shape array is an allocatable array or an ...
    (comp.lang.fortran)