473,241 Members | 1,565 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,241 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 18927
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.