Generics SortedList and DataSource

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

    Generics SortedList and DataSource

    Here is an example of a SortedList that works as a datasource to the ComboBox
    and a generic SortedList<that does not works as a datasource to the
    ComboBox.

    Why? If I use List and generic List<>, both works.

    private void Form1_Load(obje ct sender, EventArgs e)
    {
    System.Collecti ons.SortedList QA1 = new
    System.Collecti ons.SortedList( );
    QA1["Name1"] = new Query("Name1", "Group1", true);
    QA1["Name2"] = new Query("Name2", "Group1", true);
    comboBox1.DataS ource = QA1.Values; // Works !


    System.Collecti ons.Generic.Sor tedList<string, QueryQA2 = new
    System.Collecti ons.Generic.Sor tedList<string, Query>();
    QA2["Name1"] = new Query("Name1", "Group1", true);
    QA2["Name2"] = new Query("Name2", "Group1", true);
    comboBox1.DataS ource = QA2.Values; // Does not works
    }



    Here's the QUERY.cls code in case you're wondering:

    sing System;

    namespace WindowsApplicat ion1
    {
    /// <summary>
    /// Summary description for Query.
    /// </summary>
    public class Query : IComparable
    {
    string m_name ;
    string m_group;
    string m_email;
    bool m_IsDirect;


    public Query(string Name , string Group, bool IsDirect )
    {
    m_name = Name;
    m_group = Group;
    m_IsDirect = IsDirect;
    m_email = "";
    }

    public Query(string Name , string Group, bool IsDirect, string Email)
    {
    m_name = Name;
    m_group = Group;
    m_email = Email;
    m_IsDirect = IsDirect;
    }

    public string Email
    {
    get
    {
    return m_email;
    }
    set
    {
    m_email = value;
    }
    }

    public bool IsDirect
    {
    get
    {
    return m_IsDirect;
    }
    set
    {
    m_IsDirect = value;
    }
    }

    public string Name
    {
    get
    {
    return m_name;
    }
    set
    {
    m_name = value;
    }
    }

    public string Group
    {
    get
    {
    return m_group;
    }
    set
    {
    m_group = value;
    }
    }

    public string Display
    {
    get
    {
    return string.Format(" {0}{1}\t{2}",m_ IsDirect ? " \t" : "\x2206\t",
    m_group, m_name);
    }
    }
    public string DisplayALL
    {
    get
    {
    return string.Format(" {0}\t{1}\t{2}", m_group, m_name, m_email);
    }
    }

    #region IComparable Members
    // Calls CaseInsensitive Comparer.Compar e with the parameters reversed.
    // int IComparer.Compa re( Object one, Object two ) {
    // return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
    // }

    public int CompareTo(objec t obj)
    {
    if(obj is Query)
    {
    Query item = (Query) obj;
    return m_name.CompareT o(item.m_name);
    }
    throw new ArgumentExcepti on("This object is not a Query Class");
    }

    #endregion
    }
    }

  • SHEBERT

    #2
    RE: Generics SortedList and DataSource

    by the way, the error is

    "Complex DataBinding accepts as a data source either an IList or an
    IListSource."



    "SHEBERT" wrote:
    Here is an example of a SortedList that works as a datasource to the ComboBox
    and a generic SortedList<that does not works as a datasource to the
    ComboBox.
    >
    Why? If I use List and generic List<>, both works.
    >
    private void Form1_Load(obje ct sender, EventArgs e)
    {
    System.Collecti ons.SortedList QA1 = new
    System.Collecti ons.SortedList( );
    QA1["Name1"] = new Query("Name1", "Group1", true);
    QA1["Name2"] = new Query("Name2", "Group1", true);
    comboBox1.DataS ource = QA1.Values; // Works !
    >
    >
    System.Collecti ons.Generic.Sor tedList<string, QueryQA2 = new
    System.Collecti ons.Generic.Sor tedList<string, Query>();
    QA2["Name1"] = new Query("Name1", "Group1", true);
    QA2["Name2"] = new Query("Name2", "Group1", true);
    comboBox1.DataS ource = QA2.Values; // Does not works
    }
    >
    >
    >
    Here's the QUERY.cls code in case you're wondering:
    >
    sing System;
    >
    namespace WindowsApplicat ion1
    {
    /// <summary>
    /// Summary description for Query.
    /// </summary>
    public class Query : IComparable
    {
    string m_name ;
    string m_group;
    string m_email;
    bool m_IsDirect;
    >
    >
    public Query(string Name , string Group, bool IsDirect )
    {
    m_name = Name;
    m_group = Group;
    m_IsDirect = IsDirect;
    m_email = "";
    }
    >
    public Query(string Name , string Group, bool IsDirect, string Email)
    {
    m_name = Name;
    m_group = Group;
    m_email = Email;
    m_IsDirect = IsDirect;
    }
    >
    public string Email
    {
    get
    {
    return m_email;
    }
    set
    {
    m_email = value;
    }
    }
    >
    public bool IsDirect
    {
    get
    {
    return m_IsDirect;
    }
    set
    {
    m_IsDirect = value;
    }
    }
    >
    public string Name
    {
    get
    {
    return m_name;
    }
    set
    {
    m_name = value;
    }
    }
    >
    public string Group
    {
    get
    {
    return m_group;
    }
    set
    {
    m_group = value;
    }
    }
    >
    public string Display
    {
    get
    {
    return string.Format(" {0}{1}\t{2}",m_ IsDirect ? " \t" : "\x2206\t",
    m_group, m_name);
    }
    }
    public string DisplayALL
    {
    get
    {
    return string.Format(" {0}\t{1}\t{2}", m_group, m_name, m_email);
    }
    }
    >
    #region IComparable Members
    // Calls CaseInsensitive Comparer.Compar e with the parameters reversed.
    // int IComparer.Compa re( Object one, Object two ) {
    // return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
    // }
    >
    public int CompareTo(objec t obj)
    {
    if(obj is Query)
    {
    Query item = (Query) obj;
    return m_name.CompareT o(item.m_name);
    }
    throw new ArgumentExcepti on("This object is not a Query Class");
    }
    >
    #endregion
    }
    }
    >

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Generics SortedList and DataSource

      SHEBERT,

      It's exactly what the error says. The DataSource needs to implement
      IList or IListSource. The SortedList<TKey , TValueclass does not implement
      IList or IListSource interface, so it can't be used for the DataSource
      property on the ComboBox.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "SHEBERT" <SHEBERT@discus sions.microsoft .comwrote in message
      news:912CBC27-1186-4953-BF6B-98A296D5C7A2@mi crosoft.com...
      by the way, the error is
      >
      "Complex DataBinding accepts as a data source either an IList or an
      IListSource."
      >
      >
      >
      "SHEBERT" wrote:
      >
      >Here is an example of a SortedList that works as a datasource to the
      >ComboBox
      >and a generic SortedList<that does not works as a datasource to the
      >ComboBox.
      >>
      >Why? If I use List and generic List<>, both works.
      >>
      >private void Form1_Load(obje ct sender, EventArgs e)
      > {
      > System.Collecti ons.SortedList QA1 = new
      >System.Collect ions.SortedList ();
      > QA1["Name1"] = new Query("Name1", "Group1", true);
      > QA1["Name2"] = new Query("Name2", "Group1", true);
      > comboBox1.DataS ource = QA1.Values; // Works !
      >>
      >>
      > System.Collecti ons.Generic.Sor tedList<string, QueryQA2 =
      >new
      >System.Collect ions.Generic.So rtedList<string , Query>();
      > QA2["Name1"] = new Query("Name1", "Group1", true);
      > QA2["Name2"] = new Query("Name2", "Group1", true);
      > comboBox1.DataS ource = QA2.Values; // Does not works
      > }
      >>
      >>
      >>
      >Here's the QUERY.cls code in case you're wondering:
      >>
      >sing System;
      >>
      >namespace WindowsApplicat ion1
      >{
      >/// <summary>
      >/// Summary description for Query.
      >/// </summary>
      >public class Query : IComparable
      >{
      >string m_name ;
      >string m_group;
      >string m_email;
      >bool m_IsDirect;
      >>
      >>
      >public Query(string Name , string Group, bool IsDirect )
      >{
      >m_name = Name;
      >m_group = Group;
      >m_IsDirect = IsDirect;
      >m_email = "";
      >}
      >>
      >public Query(string Name , string Group, bool IsDirect, string Email)
      >{
      >m_name = Name;
      >m_group = Group;
      >m_email = Email;
      >m_IsDirect = IsDirect;
      >}
      >>
      >public string Email
      >{
      >get
      >{
      >return m_email;
      >}
      >set
      >{
      >m_email = value;
      >}
      >}
      >>
      >public bool IsDirect
      >{
      >get
      >{
      >return m_IsDirect;
      >}
      >set
      >{
      >m_IsDirect = value;
      >}
      >}
      >>
      >public string Name
      >{
      >get
      >{
      >return m_name;
      >}
      >set
      >{
      >m_name = value;
      >}
      >}
      >>
      >public string Group
      >{
      >get
      >{
      >return m_group;
      >}
      >set
      >{
      >m_group = value;
      >}
      >}
      >>
      >public string Display
      >{
      >get
      >{
      >return string.Format(" {0}{1}\t{2}",m_ IsDirect ? " \t" : "\x2206\t",
      >m_group, m_name);
      >}
      >}
      >public string DisplayALL
      >{
      >get
      >{
      >return string.Format(" {0}\t{1}\t{2}", m_group, m_name, m_email);
      >}
      >}
      >>
      >#region IComparable Members
      >// Calls CaseInsensitive Comparer.Compar e with the parameters reversed.
      >// int IComparer.Compa re( Object one, Object two ) {
      >// return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
      >// }
      >>
      >public int CompareTo(objec t obj)
      >{
      >if(obj is Query)
      >{
      >Query item = (Query) obj;
      >return m_name.CompareT o(item.m_name);
      >}
      >throw new ArgumentExcepti on("This object is not a Query Class");
      >}
      >>
      >#endregion
      >}
      >}
      >>

      Comment

      • SHEBERT

        #4
        Re: Generics SortedList and DataSource

        The LIST object works, either LIST or generics LIST<works ! Even if LIST<>
        is generics it does work as a COMBOXBOX datasource.

        then why SortedList implements ILIST (since it's working as a COMBOBOX
        DataSource) and not generics SortedList <>?

        what's the difference between SORTEDLIST and Generics SORTEDLIST<that it
        won't works?



        "Nicholas Paldino [.NET/C# MVP]" wrote:
        SHEBERT,
        >
        It's exactly what the error says. The DataSource needs to implement
        IList or IListSource. The SortedList<TKey , TValueclass does not implement
        IList or IListSource interface, so it can't be used for the DataSource
        property on the ComboBox.
        >
        Hope this helps.
        >
        >
        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m
        >
        "SHEBERT" <SHEBERT@discus sions.microsoft .comwrote in message
        news:912CBC27-1186-4953-BF6B-98A296D5C7A2@mi crosoft.com...
        by the way, the error is

        "Complex DataBinding accepts as a data source either an IList or an
        IListSource."



        "SHEBERT" wrote:
        Here is an example of a SortedList that works as a datasource to the
        ComboBox
        and a generic SortedList<that does not works as a datasource to the
        ComboBox.
        >
        Why? If I use List and generic List<>, both works.
        >
        private void Form1_Load(obje ct sender, EventArgs e)
        {
        System.Collecti ons.SortedList QA1 = new
        System.Collecti ons.SortedList( );
        QA1["Name1"] = new Query("Name1", "Group1", true);
        QA1["Name2"] = new Query("Name2", "Group1", true);
        comboBox1.DataS ource = QA1.Values; // Works !
        >
        >
        System.Collecti ons.Generic.Sor tedList<string, QueryQA2 =
        new
        System.Collecti ons.Generic.Sor tedList<string, Query>();
        QA2["Name1"] = new Query("Name1", "Group1", true);
        QA2["Name2"] = new Query("Name2", "Group1", true);
        comboBox1.DataS ource = QA2.Values; // Does not works
        }
        >
        >
        >
        Here's the QUERY.cls code in case you're wondering:
        >
        sing System;
        >
        namespace WindowsApplicat ion1
        {
        /// <summary>
        /// Summary description for Query.
        /// </summary>
        public class Query : IComparable
        {
        string m_name ;
        string m_group;
        string m_email;
        bool m_IsDirect;
        >
        >
        public Query(string Name , string Group, bool IsDirect )
        {
        m_name = Name;
        m_group = Group;
        m_IsDirect = IsDirect;
        m_email = "";
        }
        >
        public Query(string Name , string Group, bool IsDirect, string Email)
        {
        m_name = Name;
        m_group = Group;
        m_email = Email;
        m_IsDirect = IsDirect;
        }
        >
        public string Email
        {
        get
        {
        return m_email;
        }
        set
        {
        m_email = value;
        }
        }
        >
        public bool IsDirect
        {
        get
        {
        return m_IsDirect;
        }
        set
        {
        m_IsDirect = value;
        }
        }
        >
        public string Name
        {
        get
        {
        return m_name;
        }
        set
        {
        m_name = value;
        }
        }
        >
        public string Group
        {
        get
        {
        return m_group;
        }
        set
        {
        m_group = value;
        }
        }
        >
        public string Display
        {
        get
        {
        return string.Format(" {0}{1}\t{2}",m_ IsDirect ? " \t" : "\x2206\t",
        m_group, m_name);
        }
        }
        public string DisplayALL
        {
        get
        {
        return string.Format(" {0}\t{1}\t{2}", m_group, m_name, m_email);
        }
        }
        >
        #region IComparable Members
        // Calls CaseInsensitive Comparer.Compar e with the parameters reversed.
        // int IComparer.Compa re( Object one, Object two ) {
        // return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
        // }
        >
        public int CompareTo(objec t obj)
        {
        if(obj is Query)
        {
        Query item = (Query) obj;
        return m_name.CompareT o(item.m_name);
        }
        throw new ArgumentExcepti on("This object is not a Query Class");
        }
        >
        #endregion
        }
        }
        >
        >
        >
        >

        Comment

        • Claes Bergefall

          #5
          Re: Generics SortedList and DataSource

          You're binding to the object returned from the Values property, so let's
          examine what you really get using reflector

          The System.Collecti ons.SortedList. Values property is typed as returning
          ICollection, so in theory you should not be able to bind to this. However,
          what it really returns is an instance of the private type
          System.Collecti ons.SortedList. Values. This class implements both IList and
          ICollection, so binding works anyway (but this might very well change in the
          future)

          The System.Collecti ons.Generic.Sor tedList(TKey, TValue).Values is typed as
          returning IList<TValue>, so you should not be able to bind to this. But in
          reality (just as for SortedList.Valu es above) it returns an instance of the
          private type System.Collecti ons.Generic.Sor tedList(TKey,
          TValue).ValueLi st<TKey, TValue. Problem is this list doesn't implement IList
          so it cannot be used for databinding

          The generic List implements IList so it's perfectly valid as a binding
          source

          /claes

          "SHEBERT" <SHEBERT@discus sions.microsoft .comwrote in message
          news:9E30777E-14C9-4A23-AC16-B63D843EE16E@mi crosoft.com...
          The LIST object works, either LIST or generics LIST<works ! Even if
          LIST<>
          is generics it does work as a COMBOXBOX datasource.
          >
          then why SortedList implements ILIST (since it's working as a COMBOBOX
          DataSource) and not generics SortedList <>?
          >
          what's the difference between SORTEDLIST and Generics SORTEDLIST<that it
          won't works?
          >
          >
          >
          "Nicholas Paldino [.NET/C# MVP]" wrote:
          >
          >SHEBERT,
          >>
          > It's exactly what the error says. The DataSource needs to implement
          >IList or IListSource. The SortedList<TKey , TValueclass does not
          >implement
          >IList or IListSource interface, so it can't be used for the DataSource
          >property on the ComboBox.
          >>
          > Hope this helps.
          >>
          >>
          >--
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >>
          >"SHEBERT" <SHEBERT@discus sions.microsoft .comwrote in message
          >news:912CBC2 7-1186-4953-BF6B-98A296D5C7A2@mi crosoft.com...
          by the way, the error is
          >
          "Complex DataBinding accepts as a data source either an IList or an
          IListSource."
          >
          >
          >
          "SHEBERT" wrote:
          >
          >Here is an example of a SortedList that works as a datasource to the
          >ComboBox
          >and a generic SortedList<that does not works as a datasource to the
          >ComboBox.
          >>
          >Why? If I use List and generic List<>, both works.
          >>
          >private void Form1_Load(obje ct sender, EventArgs e)
          > {
          > System.Collecti ons.SortedList QA1 = new
          >System.Collect ions.SortedList ();
          > QA1["Name1"] = new Query("Name1", "Group1", true);
          > QA1["Name2"] = new Query("Name2", "Group1", true);
          > comboBox1.DataS ource = QA1.Values; // Works !
          >>
          >>
          > System.Collecti ons.Generic.Sor tedList<string, QueryQA2 =
          >new
          >System.Collect ions.Generic.So rtedList<string , Query>();
          > QA2["Name1"] = new Query("Name1", "Group1", true);
          > QA2["Name2"] = new Query("Name2", "Group1", true);
          > comboBox1.DataS ource = QA2.Values; // Does not works
          > }
          >>
          >>
          >>
          >Here's the QUERY.cls code in case you're wondering:
          >>
          >sing System;
          >>
          >namespace WindowsApplicat ion1
          >{
          >/// <summary>
          >/// Summary description for Query.
          >/// </summary>
          >public class Query : IComparable
          >{
          >string m_name ;
          >string m_group;
          >string m_email;
          >bool m_IsDirect;
          >>
          >>
          >public Query(string Name , string Group, bool IsDirect )
          >{
          >m_name = Name;
          >m_group = Group;
          >m_IsDirect = IsDirect;
          >m_email = "";
          >}
          >>
          >public Query(string Name , string Group, bool IsDirect, string Email)
          >{
          >m_name = Name;
          >m_group = Group;
          >m_email = Email;
          >m_IsDirect = IsDirect;
          >}
          >>
          >public string Email
          >{
          >get
          >{
          >return m_email;
          >}
          >set
          >{
          >m_email = value;
          >}
          >}
          >>
          >public bool IsDirect
          >{
          >get
          >{
          >return m_IsDirect;
          >}
          >set
          >{
          >m_IsDirect = value;
          >}
          >}
          >>
          >public string Name
          >{
          >get
          >{
          >return m_name;
          >}
          >set
          >{
          >m_name = value;
          >}
          >}
          >>
          >public string Group
          >{
          >get
          >{
          >return m_group;
          >}
          >set
          >{
          >m_group = value;
          >}
          >}
          >>
          >public string Display
          >{
          >get
          >{
          >return string.Format(" {0}{1}\t{2}",m_ IsDirect ? " \t" : "\x2206\t",
          >m_group, m_name);
          >}
          >}
          >public string DisplayALL
          >{
          >get
          >{
          >return string.Format(" {0}\t{1}\t{2}", m_group, m_name, m_email);
          >}
          >}
          >>
          >#region IComparable Members
          >// Calls CaseInsensitive Comparer.Compar e with the parameters reversed.
          >// int IComparer.Compa re( Object one, Object two ) {
          >// return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
          >// }
          >>
          >public int CompareTo(objec t obj)
          >{
          >if(obj is Query)
          >{
          >Query item = (Query) obj;
          >return m_name.CompareT o(item.m_name);
          >}
          >throw new ArgumentExcepti on("This object is not a Query Class");
          >}
          >>
          >#endregion
          >}
          >}
          >>
          >>
          >>
          >>

          Comment

          Working...