473,395 Members | 1,670 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,395 software developers and data experts.

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.datamember1 = dataMember1;}

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

public bool TrueOrFalse(string argument) {return false;}

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

public class MyClassCollection: 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.IndexOf(classKey)] = value;}
get {return (MyClass) this.List[this.List.IndexOf(classKey)];}
}
}//public class MyClassCollection: CollectionBase

This string indexer doesn't work as 'this.List.IndexOf(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 1843
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 IndexOutOfRangeException();
}
}

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 NameOjectCollectionBase, but that might
not offer quite the functionality you want. (In particular, since
NameObjectCollectionBase stores the key separately from the object, you would
need to implement a DataMember1Changed 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.datamember1 = dataMember1;}

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

public bool TrueOrFalse(string argument) {return false;}

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

public class MyClassCollection: 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.IndexOf(classKey)] = value;}
get {return (MyClass) this.List[this.List.IndexOf(classKey)];}
}
}//public class MyClassCollection: CollectionBase

This string indexer doesn't work as 'this.List.IndexOf(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 MyClassCollection: 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.IndexOf(classKey)] = value;}
get {return (MyClass) this.List[this.List.IndexOf(classKey)];}
}
}//public class MyClassCollection: CollectionBase

This string indexer doesn't work as 'this.List.IndexOf(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.com>
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.Count; i++) {if
(this[i].DataMemeber1.Equals(paramString) {...; break;}} Or,
foreach(MyClass mc in this) {if (mc.DataMember1.Equals(paramString) {...;
break;}}....

Thanks again...

Regards,
SpotNet.
"SpotNet" <Sp*****@msnews.grp> wrote in message
news:e0****************@TK2MSFTNGP10.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.datamember1 = dataMember1;}
:
: public void AGreatFunction() {//The usual stuff}
:
: public bool TrueOrFalse(string argument) {return false;}
:
: public string DataMember1
: {get {return this.datamember1;}
: }//public class MyClass
:
: public class MyClassCollection: 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.IndexOf(classKey)] = value;}
: get {return (MyClass) this.List[this.List.IndexOf(classKey)];}
: }
: }//public class MyClassCollection: CollectionBase
:
: This string indexer doesn't work as 'this.List.IndexOf(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.Count; i++) {if
(this[i].DataMemeber1.Equals(paramString) {...; break;}} Or,
foreach(MyClass mc in this) {if (mc.DataMember1.Equals(paramString) {...;
break;}}....

Thanks again...

Regards,
SpotNet.
"SpotNet" <Sp*****@msnews.grp> wrote in message
news:e0****************@TK2MSFTNGP10.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.datamember1 = dataMember1;}
:
: public void AGreatFunction() {//The usual stuff}
:
: public bool TrueOrFalse(string argument) {return false;}
:
: public string DataMember1
: {get {return this.datamember1;}
: }//public class MyClass
:
: public class MyClassCollection: 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.IndexOf(classKey)] = value;}
: get {return (MyClass) this.List[this.List.IndexOf(classKey)];}
: }
: }//public class MyClassCollection: CollectionBase
:
: This string indexer doesn't work as 'this.List.IndexOf(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***@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.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.Count; i++) {if
: > (this[i].DataMemeber1.Equals(paramString) {...; break;}} Or,
: > foreach(MyClass mc in this) {if (mc.DataMember1.Equals(paramString)
{...;
: > break;}}....
: >
: > Thanks again...
: >
: > Regards,
: > SpotNet.
: > "SpotNet" <Sp*****@msnews.grp> wrote in message
: > news:e0****************@TK2MSFTNGP10.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.datamember1 =
dataMember1;}
: > :
: > : public void AGreatFunction() {//The usual stuff}
: > :
: > : public bool TrueOrFalse(string argument) {return false;}
: > :
: > : public string DataMember1
: > : {get {return this.datamember1;}
: > : }//public class MyClass
: > :
: > : public class MyClassCollection: 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.IndexOf(classKey)] = value;}
: > : get {return (MyClass) this.List[this.List.IndexOf(classKey)];}
: > : }
: > : }//public class MyClassCollection: CollectionBase
: > :
: > : This string indexer doesn't work as 'this.List.IndexOf(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
by: A.M | last post by:
Hi, I have a property in my class like this: public string CurrentDisplayName { get { ... return s;
5
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 =...
5
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...
17
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
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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...

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.