Using property to access elements of array?

P

pinetaj

Hello,
I have a question of using 'property' on accessing elements of array.
There is an array member in a class. I'd like to restrict accessing the
elements of the array through property. And also by annotating the set
method of the property with user-defined attribute, i'd like to
activate something whenever the set method is invoked.
I wrote the code as the followings. But, my problem is that it allows
to set a value on an element of the array using property but the set
method is never invoked.
In the following example, PARRINT1 is a property for an array.
I expected that when A1.PARRINT1[1] = 16 is called, the set method of
PARRINT1 is invoked but actually it's not. Actually when A1.PARRINT1 =
new int[4]{21,22,23,24}; is called, the set method is invoked. Is there
any way to make the set method be invoked when the elements of the
array are set?

I checked indexer but that's not what I want.

I appreciate any your advice.
------------

class CTestA
{
priate int[] arrInt1 = new int[4]{11, 12, 13, 14};
public int[] PARRINT1
{
get
{
return arrInt1;
}
[SetAttr]
set
{
arrInt1 = value;
}
}

}//class CTestA
class Test{
static void Main(string[] args)
{
A1 = new CTestA();
A1.PARRINT1[1] = 16;
A1.PARRINT1 = new int[4]{21,22,23,24};
A1.I = 41;
}
} //class Test
 
N

Nicholas Paldino [.NET/C# MVP]

pinetaj,

Actually, you do want an indexer, but not on the class that exposes the
property. What you want to do is create a wrapper class which exposes the
array elements through an indexer, then expose an instance of that class
from your main class. That way, you can write code that is called when you
access the indexer, as well as have a property that looks like an exposed
array.

Hope this helps.
 
M

Michael Bray

Oh man where do I start???

1) You are providing a set and get accessor, but you are setting the
private array to the value passed. This is no better than just making the
array a public property. This is a completely improper way to use accessor
methods. If you want to restrict access to a private array, then in this
case you'll probably need several functions - one to create an array of the
size you need and possibly with the initial values, one to access the
members of the function, one to... there's a bunch of things.

2) If you want to invoke a function when one of the array values is set,
then you'll need a specific method to set a particular value in the array.
From this method you can fire an event. With the code you have shown,
there is no way to know when code outside of CTestA modifies the members of
the array.

I could go on and on, but I think I'll stop here.

-mdb

Hello,
I have a question of using 'property' on accessing elements of array.
There is an array member in a class. I'd like to restrict accessing the
[snip]
 
P

Paul Henderson

But, my problem is that it allows
to set a value on an element of the array using property but the set
method is never invoked.

The set method will only be called when the property itself is assigned
to, i.e. when you assign it to an array object; when you set an
element, you do not change the pointer to the array itself, only the
value of a specific member, hence set is not called.
I checked indexer but that's not what I want.

Well, it might not be what you *want*, but it could perhaps be done
easiest with indexers, if you make a class that exists just to contain
one array, and exposes a get/set indexer with your custom code in it.
Then, instead of PARRINT1 being an int[], make it an instance of the
class. That would also provide a neater way of initialising the array
(as you could just have a one-parameter constructor that did it).
 
G

Guest

It sounds like you are looking for an indexer:

public object this [int index]
{
get
{
return arrInt1[index];
}

set
{
arrInt1[index]=(int)value;
}
}



Usage:
CTestA A1 = new CTestA();
A1.PARRINT1 = new int[4]{21,22,23,24};
A1[1] = 41;
Console.WriteLine(A1[1]);

Peter
 
P

pinetaj

Thanks for the reply.
Seems that I can't avoid using indexer.
What I want to do is that either setting the element of the array or
setting the array itself can trigger certain event through set method
(either a set method of indexer or a set method of property). And it's
better if they can use same interface. The set method is annotated with
certain attribute.
But, seems that i can not define an indexer for set method to array
itself. And also i can't define a property of setting elements. So, the
following (named as working_code) is what I compromise by combining a
property and indexer even though I don't like it much.

Questions:
1) can i define 'this' method into the indexer without index like the
following?
public int[] this
{
get { return dat; }
[SetAtt]
set { data[index] = value; }
}//this

2) With the combination of property and indexer, accesing elements and
accesing the array has two different interface like the folloing.
A1.arrInt1[1] = 16;
A1.ARRINT1 = B1;
Any better ideas?

I appreciate your advice.

==working_code:
class PARRINT
{
private int []data ;

public PARRINT(int[] param1){
data = param1;
}
public int this [int index]
{
get
{
return data[index];
}
[SetAtt]
set
{
data[index] = value;
}
}//this
}
class CTestA
{
{
public PARRINT arrInt1 ;
public PARRINT ARRINT1
{
get
{
return arrInt1;
}
[SetAtt]
set
{
arrInt1 = value;
}
}

}//class CTestA
class SPersistentTest
{
static void Main(string[] args)
{
CTestA A1 = new CTestA();
PARRINT B1 = new PARRINT(new int[4]{21, 22, 23, 24});

A1.ARRINT1 = new PARRINT(new int[4]{11, 12, 13, 14});
A1.arrInt1[1] = 16;
A1.ARRINT1 = B1;
}//Main
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top