473,790 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Indexer question

I'm trying to define a custom class indexer to take the
place of an underlying Hashtable member so that values
can be referred to as Class1[index] instead of
Class1.someVar[index].

The following code does not work & I believe I understand
why (a value is returned by my indexer, not a reference).
I have a feeling I'm just using this indexer horribly
wrong, but any insight would be much appreciated!

//Class 1 definition
class ClownCar{
private Hashtable Clowns = new Hashtable();

//...code omitted

//Properties
public Clown this [int index] {
get {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Inde x outside the range of
valid values.");
else
return (Clown)Clowns[index];
}
set {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Inde x outside the range of
valid values.");
else
Clown[index] = value;
}
}
}
//Class 2 definition
class Clown{
bool CanJuggle = true;
//...code omitted
}
//client script tries to set an indexer's data

ClownCar RedClownCar = new ClownCar();

//...code omitted

//the following line of code does jack sh*t to the
referenced object (i.e. varClass[0]'s internal values
will remain the same).

RedClownCar[0].CanJuggle= false;
Jerry Negrelli
Nov 15 '05 #1
2 1414
Jerry,

If you are providing lookup functionality, then you will want to derive
your class from the DictionaryBase class. It will provide a good deal of
the functionality for constructs of this type.

However, it looks like what you want is Collection functionality,
because you are not keying on any sort of key, but rather an index in the
collection. Because of this, you should extend from CollectionBase.

Clown is defined as a class, so when you do something like this for your
colletion:

ClownCar[0].CanJuggle = true;

It should work, because ClownCar is returning a reference type.
However, the behavior you are describing would occur if Clown was a
structure (value type).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jerry Negrelli" <je************ @nospam.datasci entific.com> wrote in message
news:10******** *************** *****@phx.gbl.. .
I'm trying to define a custom class indexer to take the
place of an underlying Hashtable member so that values
can be referred to as Class1[index] instead of
Class1.someVar[index].

The following code does not work & I believe I understand
why (a value is returned by my indexer, not a reference).
I have a feeling I'm just using this indexer horribly
wrong, but any insight would be much appreciated!

//Class 1 definition
class ClownCar{
private Hashtable Clowns = new Hashtable();

//...code omitted

//Properties
public Clown this [int index] {
get {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Inde x outside the range of
valid values.");
else
return (Clown)Clowns[index];
}
set {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Inde x outside the range of
valid values.");
else
Clown[index] = value;
}
}
}
//Class 2 definition
class Clown{
bool CanJuggle = true;
//...code omitted
}
//client script tries to set an indexer's data

ClownCar RedClownCar = new ClownCar();

//...code omitted

//the following line of code does jack sh*t to the
referenced object (i.e. varClass[0]'s internal values
will remain the same).

RedClownCar[0].CanJuggle= false;
Jerry Negrelli

Nov 15 '05 #2
Clown is definitely a class, not a struct, so shouldn't
the following two commands yield the same result? Does
indexing the ClownCar class interfere with this?

//does not work
ClownCar[0].CanJuggle = true;

//works, if I simply change the "Clowns" Hashtable
to "public"
((Clown)ClownCa r.Clowns[0]).CanJuggle = true;
ps- there was a typo in my "get" accessor, "Clown[i]"
should have been "Clowns[i]", so hopefully that did not
create any confusion.
-----Original Message-----
Jerry,

If you are providing lookup functionality, then you will want to deriveyour class from the DictionaryBase class. It will provide a good deal ofthe functionality for constructs of this type.

However, it looks like what you want is Collection functionality,because you are not keying on any sort of key, but rather an index in thecollection. Because of this, you should extend from CollectionBase.
Clown is defined as a class, so when you do something like this for yourcolletion:

ClownCar[0].CanJuggle = true;

It should work, because ClownCar is returning a reference type.However, the behavior you are describing would occur if Clown was astructure (value type).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jerry Negrelli" <je************ @nospam.datasci entific.com> wrote in
messagenews:10******* *************** ******@phx.gbl. ..
I'm trying to define a custom class indexer to take the
place of an underlying Hashtable member so that values
can be referred to as Class1[index] instead of
Class1.someVar[index].

The following code does not work & I believe I understand why (a value is returned by my indexer, not a reference). I have a feeling I'm just using this indexer horribly
wrong, but any insight would be much appreciated!

//Class 1 definition
class ClownCar{
private Hashtable Clowns = new Hashtable();

//...code omitted

//Properties
public Clown this [int index] {
get {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Inde x outside the range of
valid values.");
else
return (Clown)Clowns[index];
}
set {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Inde x outside the range of
valid values.");
else
Clowns[index] = value;
}
}
}
//Class 2 definition
class Clown{
bool CanJuggle = true;
//...code omitted
}
//client script tries to set an indexer's data

ClownCar RedClownCar = new ClownCar();

//...code omitted

//the following line of code does jack sh*t to the
referenced object (i.e. varClass[0]'s internal values
will remain the same).

RedClownCar[0].CanJuggle= false;
Jerry Negrelli

.

Nov 15 '05 #3

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

Similar topics

14
8375
by: Nikhil Patel | last post by:
Hi all, Is it possible to have more than one indexer in a class. If not, are there any good alternative ways to achieve similar functionality. Thanks... -Nikhil
4
1696
by: Dotnetjunky | last post by:
Hi, I'm really get confused on this issue. Hope that someone can help me get clear after this post. Take the class System.Web.Caching.Cache for example. In the documentation, there's a property called Item and the description is: "Gets or sets the cache item at the specified key. In C#, this property is the indexer for the Cache class."
3
2536
by: george r smith | last post by:
I am converting a delphi chess program to C#, this is how I study a new language and I have the following problem. How do you use an enumeration value as an indexer for an array ? This is what I have come up with, but it seems vebose. In Delphi I would just say PieceValue instead of PieceValue. This is ok if it is the only way but is there a better C# way. thanks
7
485
by: Oren | last post by:
hi I have created a class with a simple indexer what is the syntax for adding the function "Remove" ... myClass.colors=Color.Black; // WORKS O myClass.colors.Remove(); // ?? .... ColorCollection _colors=new ColorCollection() public ColorCollection Color
7
4621
by: Steph | last post by:
I'm learning C#. I need to implement an indexer using an array list. I have the following code and I'm getting an error "Inconsistent accessibility: indexer return type CRegs is less accessible than indexer Whouse.this. Would someone please tell me why? public class Whouse : IEnumerable public CRegs this // Indexer { get
5
1859
by: SpotNet | last post by:
Hello NewsGroup, I have a custom class and a collection for that custom class that inherits CollectionBase. As such; public class MyClass { private string datamember1 = string.Empty, datamember2 = string.Empty; private int datamember3 = -1;
3
1479
by: Nash Alexx | last post by:
Hello! I am quite new to C#, and one concept that really gives me a headache is "indexer". I have gone through the MSDN examples, and, at some level know how to use indexers. But, the thing is I do not understand the specification that says something along this line "with indexers you can index objects of a class like an array". The thing is, in all the examples, there is only one "new" call when only one object is created! There is no...
17
2093
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
2
1982
by: hufaunder | last post by:
SITUATION: I have a class "MainClass" that has an indexer "SubClass1Indexer". This indexer returns an instance that implements a particular interface "ISubClass1". This interface defines another indexer "SubClass2Indexer". Bellow is some code with comments. It should compile. PROBLEM: I would like to write something like this:
1
10145
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7530
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
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 we have to send another system
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.