Re: Is it possible to have parameters in a property?
- From: Stu <stumorgan@xxxxxxxxx>
- Date: Wed, 11 Nov 2009 12:03:40 -0800 (PST)
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.
.
- References:
- Is it possible to have parameters in a property?
- From: Stu
- Re: Is it possible to have parameters in a property?
- From: Alberto Poblacion
- Re: Is it possible to have parameters in a property?
- From: Stu
- Is it possible to have parameters in a property?
- Prev by Date: Re: Is it possible to have parameters in a property?
- Next by Date: linq to sql update not working for one column
- Previous by thread: Re: Is it possible to have parameters in a property?
- Next by thread: Re: Is it possible to have parameters in a property?
- Index(es):
Relevant Pages
|