String parameter indexer question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SpotNet

    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.


  • Phil N

    #2
    RE: String parameter indexer question.

    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:
    [color=blue]
    >
    > 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.
    >
    >
    >[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: String parameter indexer question.

      SpotNet <SpotNet@msnews .grp> wrote:[color=blue]
      > 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.[/color]

      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 - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • SpotNet

        #4
        Re: String parameter indexer question.


        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" <SpotNet@msnews .grp> wrote in message
        news:e0P%231RCX FHA.3092@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.
        :
        :


        Comment

        • Phil N

          #5
          Re: String parameter indexer question.

          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:
          [color=blue]
          >
          > 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" <SpotNet@msnews .grp> wrote in message
          > news:e0P%231RCX FHA.3092@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.
          > :
          > :
          >
          >
          >[/color]

          Comment

          • SpotNet

            #6
            Re: String parameter indexer question.

            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" <PhilN@discussi ons.microsoft.c om> wrote in message
            news:A9414BC1-39D8-4102-B95C-8FEADC9E2B7B@mi crosoft.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" <SpotNet@msnews .grp> wrote in message
            : > news:e0P%231RCX FHA.3092@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.
            : > :
            : > :
            : >
            : >
            : >


            Comment

            Working...