473,804 Members | 3,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String parameter indexer question.


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;

public MyClass(string dataMember1) {this.datamembe r1 = dataMember1;}

public void AGreatFunction( ) {//The usual stuff}

public bool TrueOrFalse(str ing argument) {return false;}

public string DataMember1
{get {return this.datamember 1;}
}//public class MyClass

public class MyClassCollecti on: CollectionBase
{
public MyClass this[int index] //How well this works
{
set {this.List[index] = value;}
get {return (MyClass) this.List[Index];}
}

public MyClass this[string classKey] //This doesn't work
{
set {this.List[this.List.Index Of(classKey)] = value;}
get {return (MyClass) this.List[this.List.Index Of(classKey)];}
}
}//public class MyClassCollecti on: CollectionBase

This string indexer doesn't work as 'this.List.Inde xOf(classKey)' always
returns -1. Can someone tell me please what I must consider to get a string
indexer working for this situation? Many thanks news group.

Regards,
SpotNet.
Nov 17 '05 #1
5 1859
CollectionBase. IndexOf( object value ) searches for 'value' as an item within
the list, whereas it appears that you're trying to search for an object that
has a property equal to 'value.'

To get the functionality you want, if I'm understanding you correctly, you
have to search the list.

public virtual MyClass this[ string dataMember1 ]
{
get
{
for ( int i = 0; i < List.Count; i++ )
{
if( this[ i ].DataMember1 == dataMember1 )
{
return this[ i ];
}
}
throw new IndexOutOfRange Exception();
}
}

The linear search above is the method that the framework uses for the
ArrayList inside your CollectionBase (well, all right, the framework's
version might be cleaner, but I'm not near VS at the moment). If your list
can be sorted, more efficient methods may be available; Google "array search
algorithms."

You may also be able to derive from NameOjectCollec tionBase, but that might
not offer quite the functionality you want. (In particular, since
NameObjectColle ctionBase stores the key separately from the object, you would
need to implement a DataMember1Chan ged event in MyClass and handle it in the
collection . . . and add and remove handlers as collection items are added
and removed.)

HTH.

"SpotNet" wrote:

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;

public MyClass(string dataMember1) {this.datamembe r1 = dataMember1;}

public void AGreatFunction( ) {//The usual stuff}

public bool TrueOrFalse(str ing argument) {return false;}

public string DataMember1
{get {return this.datamember 1;}
}//public class MyClass

public class MyClassCollecti on: CollectionBase
{
public MyClass this[int index] //How well this works
{
set {this.List[index] = value;}
get {return (MyClass) this.List[Index];}
}

public MyClass this[string classKey] //This doesn't work
{
set {this.List[this.List.Index Of(classKey)] = value;}
get {return (MyClass) this.List[this.List.Index Of(classKey)];}
}
}//public class MyClassCollecti on: CollectionBase

This string indexer doesn't work as 'this.List.Inde xOf(classKey)' always
returns -1. Can someone tell me please what I must consider to get a string
indexer working for this situation? Many thanks news group.

Regards,
SpotNet.

Nov 17 '05 #2
SpotNet <Sp*****@msnews .grp> wrote:
public class MyClassCollecti on: CollectionBase
{
public MyClass this[int index] //How well this works
{
set {this.List[index] = value;}
get {return (MyClass) this.List[Index];}
}

public MyClass this[string classKey] //This doesn't work
{
set {this.List[this.List.Index Of(classKey)] = value;}
get {return (MyClass) this.List[this.List.Index Of(classKey)];}
}
}//public class MyClassCollecti on: CollectionBase

This string indexer doesn't work as 'this.List.Inde xOf(classKey)' always
returns -1. Can someone tell me please what I must consider to get a string
indexer working for this situation? Many thanks news group.


Well, what do you expect it to do? It's currently looking for a string
in the list, and there aren't any strings in the list - just instances
of MyClass. Are you wanting it to match on DataMember1?

Unfortunately IList itself doesn't support searching with an IComparer,
which is effectively what you want to do, but you could always go
through the list manually and check each element.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

Hello Phil N, Jon,

Thanks for the response and good advice. I didn't articulate myself all that
well, primarily I wanted (obviously) to have a string parameter indexer as
well as an integer one. I discovered the hard way what I posted didn't work,
I wanted to know if a string parameter indexer was possible using the
CollectionBase parent or base.

I've implemented your solutions (works well indeed thanks) and wanted to ask
in this situation, which would be better to use;

for (int idx = 0; idx < mycollection.Co unt; i++) {if
(this[i].DataMemeber1.E quals(paramStri ng) {...; break;}} Or,
foreach(MyClass mc in this) {if (mc.DataMember1 .Equals(paramSt ring) {...;
break;}}....

Thanks again...

Regards,
SpotNet.
"SpotNet" <Sp*****@msnews .grp> wrote in message
news:e0******** ********@TK2MSF TNGP10.phx.gbl. ..
:
: 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;
:
: public MyClass(string dataMember1) {this.datamembe r1 = dataMember1;}
:
: public void AGreatFunction( ) {//The usual stuff}
:
: public bool TrueOrFalse(str ing argument) {return false;}
:
: public string DataMember1
: {get {return this.datamember 1;}
: }//public class MyClass
:
: public class MyClassCollecti on: CollectionBase
: {
: public MyClass this[int index] //How well this works
: {
: set {this.List[index] = value;}
: get {return (MyClass) this.List[Index];}
: }
:
: public MyClass this[string classKey] //This doesn't work
: {
: set {this.List[this.List.Index Of(classKey)] = value;}
: get {return (MyClass) this.List[this.List.Index Of(classKey)];}
: }
: }//public class MyClassCollecti on: CollectionBase
:
: This string indexer doesn't work as 'this.List.Inde xOf(classKey)' always
: returns -1. Can someone tell me please what I must consider to get a
string
: indexer working for this situation? Many thanks news group.
:
: Regards,
: SpotNet.
:
:
Nov 17 '05 #4
It depends on your application, and there's a bit of a catch 22 here.
Enumerating over the collection (using foreach) introduces heap and virtual
function overhead that isn't present with the linear search (using for), so
for will reduce the application's resource consumption as the size of the
collection increases. The catch: a linear search really begins to waste
processor cycles on large collections anyway. Think here of how much is
wasted by using a linear search to find "Zebra" in an unabridged dictionary,
for example.

If your collection might get really large, reply and say so, and I'll give
you some tips on improving the performance.

"SpotNet" wrote:

Hello Phil N, Jon,

Thanks for the response and good advice. I didn't articulate myself all that
well, primarily I wanted (obviously) to have a string parameter indexer as
well as an integer one. I discovered the hard way what I posted didn't work,
I wanted to know if a string parameter indexer was possible using the
CollectionBase parent or base.

I've implemented your solutions (works well indeed thanks) and wanted to ask
in this situation, which would be better to use;

for (int idx = 0; idx < mycollection.Co unt; i++) {if
(this[i].DataMemeber1.E quals(paramStri ng) {...; break;}} Or,
foreach(MyClass mc in this) {if (mc.DataMember1 .Equals(paramSt ring) {...;
break;}}....

Thanks again...

Regards,
SpotNet.
"SpotNet" <Sp*****@msnews .grp> wrote in message
news:e0******** ********@TK2MSF TNGP10.phx.gbl. ..
:
: 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;
:
: public MyClass(string dataMember1) {this.datamembe r1 = dataMember1;}
:
: public void AGreatFunction( ) {//The usual stuff}
:
: public bool TrueOrFalse(str ing argument) {return false;}
:
: public string DataMember1
: {get {return this.datamember 1;}
: }//public class MyClass
:
: public class MyClassCollecti on: CollectionBase
: {
: public MyClass this[int index] //How well this works
: {
: set {this.List[index] = value;}
: get {return (MyClass) this.List[Index];}
: }
:
: public MyClass this[string classKey] //This doesn't work
: {
: set {this.List[this.List.Index Of(classKey)] = value;}
: get {return (MyClass) this.List[this.List.Index Of(classKey)];}
: }
: }//public class MyClassCollecti on: CollectionBase
:
: This string indexer doesn't work as 'this.List.Inde xOf(classKey)' always
: returns -1. Can someone tell me please what I must consider to get a
string
: indexer working for this situation? Many thanks news group.
:
: Regards,
: SpotNet.
:
:

Nov 17 '05 #5
Hello Phil N,

Thanks for the explanation. The collection I'm dealing with is actually
small with the remote possibility that it might be large. The reason I asked
was due to some C# literature I read comparing the 'for' with the 'foreach'
loop statements. The loops cycled through objects expressed as an object
array in the 'for' case then object enumeration using 'foreach'. The
conclusions being that when looping through objects its' best to use
foreach, then I heard otherwise which is my main reason for asking. I didn't
really think it was size (number of objects) based until now. Thank you very
much for that.

Regards,
SpotNet.

"Phil N" <Ph***@discussi ons.microsoft.c om> wrote in message
news:A9******** *************** ***********@mic rosoft.com...
: It depends on your application, and there's a bit of a catch 22 here.
: Enumerating over the collection (using foreach) introduces heap and
virtual
: function overhead that isn't present with the linear search (using for),
so
: for will reduce the application's resource consumption as the size of the
: collection increases. The catch: a linear search really begins to waste
: processor cycles on large collections anyway. Think here of how much is
: wasted by using a linear search to find "Zebra" in an unabridged
dictionary,
: for example.
:
: If your collection might get really large, reply and say so, and I'll give
: you some tips on improving the performance.
:
: "SpotNet" wrote:
:
: >
: > Hello Phil N, Jon,
: >
: > Thanks for the response and good advice. I didn't articulate myself all
that
: > well, primarily I wanted (obviously) to have a string parameter indexer
as
: > well as an integer one. I discovered the hard way what I posted didn't
work,
: > I wanted to know if a string parameter indexer was possible using the
: > CollectionBase parent or base.
: >
: > I've implemented your solutions (works well indeed thanks) and wanted to
ask
: > in this situation, which would be better to use;
: >
: > for (int idx = 0; idx < mycollection.Co unt; i++) {if
: > (this[i].DataMemeber1.E quals(paramStri ng) {...; break;}} Or,
: > foreach(MyClass mc in this) {if (mc.DataMember1 .Equals(paramSt ring)
{...;
: > break;}}....
: >
: > Thanks again...
: >
: > Regards,
: > SpotNet.
: > "SpotNet" <Sp*****@msnews .grp> wrote in message
: > news:e0******** ********@TK2MSF TNGP10.phx.gbl. ..
: > :
: > : 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;
: > :
: > : public MyClass(string dataMember1) {this.datamembe r1 =
dataMember1;}
: > :
: > : public void AGreatFunction( ) {//The usual stuff}
: > :
: > : public bool TrueOrFalse(str ing argument) {return false;}
: > :
: > : public string DataMember1
: > : {get {return this.datamember 1;}
: > : }//public class MyClass
: > :
: > : public class MyClassCollecti on: CollectionBase
: > : {
: > : public MyClass this[int index] //How well this works
: > : {
: > : set {this.List[index] = value;}
: > : get {return (MyClass) this.List[Index];}
: > : }
: > :
: > : public MyClass this[string classKey] //This doesn't work
: > : {
: > : set {this.List[this.List.Index Of(classKey)] = value;}
: > : get {return (MyClass) this.List[this.List.Index Of(classKey)];}
: > : }
: > : }//public class MyClassCollecti on: CollectionBase
: > :
: > : This string indexer doesn't work as 'this.List.Inde xOf(classKey)'
always
: > : returns -1. Can someone tell me please what I must consider to get a
: > string
: > : indexer working for this situation? Many thanks news group.
: > :
: > : Regards,
: > : SpotNet.
: > :
: > :
: >
: >
: >
Nov 17 '05 #6

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

Similar topics

3
1270
by: A.M | last post by:
Hi, I have a property in my class like this: public string CurrentDisplayName { get { ... return s;
5
5866
by: gmccallum | last post by:
I am trying to convert the value of a string to a defined enum value such as follows. public enum MyEnum { One, Two }; string MyString = "One"; // or even this is fine string MyString2 = "MyEnum.One"; // This doesn't work (obviously), but effectively this
5
17669
by: rwoo_98 | last post by:
I am trying to pass a reference to a method --SomeMethod. This table resides in a dataset and I refer to the datatable in the dataset using an indexer. However when I attempt to do this I get the error message, "A property or indexer may not be passed as an out or ref parameter?" Here is the code SomeMethod(ref ds_User.Tables);
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,
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
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
7608
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
5503
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...
1
4277
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
3
2975
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.