473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Using property to access elements of array?

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

Jan 17 '06 #1
6 18934
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"pinetaj" <ok****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Jan 17 '06 #2
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

"pinetaj" <ok****@gmail.com> wrote in news:1137518868.586250.228980
@g43g2000cwa.googlegroups.com:
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]
Jan 17 '06 #3
> 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).

Jan 17 '06 #4
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"pinetaj" wrote:
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

Jan 17 '06 #5
Namratha Shah has a nice little article about multiple indexers using
interfaces here:
http://www.codeproject.com/useritems/Indexers.asp
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"pinetaj" wrote:
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

Jan 17 '06 #6
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
}

Jan 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: Paul | last post by:
Please help, I am working on an Active-x project involving Arrays with up to 20 elements populated in a class module. In order to keep programming and debugging simple, Instead of refering to...
1
by: Kenneth | last post by:
This is probably very simple question but how do you access elements inside the <div> tag? I need to access the value of a textfield inside a <div> tag. Thanks.
1
by: tmaster | last post by:
Within a class, can I create a property that is a listview? Here's what I tried, but it doesn't seem to work: '------------ create property to give the second form access to the first form's...
0
by: thecoolone | last post by:
I am creating a REST API in php that parses the XML result given by IBM Omnifind. here's the php code: $query=$_GET;...
7
by: Curious | last post by:
Hi, I have a C# class that contains a property defined as follows: public bool bRunTwice { get { return bRunTwice; } set { bRunTwice = value; } }
5
by: emmanuel.rivoire | last post by:
Hello, I spent almost a week to be able to embed Python within my C++ game engine. I wrote a mini-tutorial of what I was able to do so far here :...
1
by: ncsthbell | last post by:
I created a database using full blown access 2007. I have put it out for users to grab and test using Runtime Access 2007. They have entered data and now I need to go back into a table and change a...
1
by: p4willi | last post by:
Can I send a message in continuation with this one? I have a sub form with that creates an array in the following manner: Dim i As Integer Dim Arr1() As String Private Sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.