473,473 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Property with indexers

I have a class and I want to create a property but I need it work as an
indexer. For example:

pupil.calification[3] = 8;

Can you tell me how to do it?
Thank you very much.
Nov 16 '05 #1
3 1100
You want syntax like this in your pupil class...
int this[int index]
{
set
{
// use index and value to set your data
}
}
"Alberto" <al*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class and I want to create a property but I need it work as an
indexer. For example:

pupil.calification[3] = 8;

Can you tell me how to do it?
Thank you very much.

Nov 16 '05 #2
Hi Alberto,

"Alberto" <al*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class and I want to create a property but I need it work as an
indexer. For example:

pupil.calification[3] = 8;

Can you tell me how to do it?
Thank you very much.


The calification property will need to return an object that defines an
indexer (indexers can only be defined for classes, not properties, methods,
etc.).

Regards,
Daniel
Nov 16 '05 #3
A full example might be:

public class Pupil {
public int this[int index] {
get { }
set { }
}
}

In the above case you can do:

Pupil p = new Pupil();
p[3] = 8;

However, that isn't what you want. You need it on the property.

public class Calification {
public int this[int index] {
get { } // Implement these
set { } // Implement these
}
}

public class Pupil {
private Calification calification = new Calification();
public Calification Calification {
get { return this.calification; }
}
}

Now you can call:

Pupil p = new Pupil();
p.Calification[3] = 8;

The Calification class is going to be holding your data in this instance. You
may
want to nest this class within your Pupil class such that it behaves more like a
member and you can enforce data protection.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Alberto" <al*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class and I want to create a property but I need it work as an
indexer. For example:

pupil.calification[3] = 8;

Can you tell me how to do it?
Thank you very much.

Nov 16 '05 #4

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

Similar topics

1
by: Andrew Chen | last post by:
Hi: I've tried to use Hashtable.Item property, which should return a value according to the key assigned. But it returns an error saying the function does not exist in the class (it did not...
1
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. ...
2
by: Nad | last post by:
Good day, I have a question about the Page.Session property used in ASP.NET This property is defined as follows: public virtual HttpSessionState Session {get;} and HttpSessionState is a...
5
by: fred | last post by:
With a Class is there any difference between a readonly property and function besides having to use Get/End Get. Are there any performance/resource advantages for either. Thanks Fred
8
by: Rainer Queck | last post by:
Hi NG, how can I implement more then one array property to a class? I have read about the Indexer, but as far as I can see this would only work for a Collection type of class like list or...
6
by: pinetaj | last post by:
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...
3
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its...
7
by: Tom Dacon | last post by:
I'm using Reflection to iterate over the properties and fields of an arbitrary object. For non-indexed properties it's pi.GetValue(theObject, Nothing) for VB, or pi.GetValue(theObject, null) for...
0
by: christian2.schmidt | last post by:
Hi, I tried to implement the std::swap method on properties (and indexers) without success. The problem is that list calls the getter and returns a copy and the assignment in the swap method...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.