Solved

IList and DictionaryBase

Posted on 2004-10-29
1,027 Views
Last Modified: 2008-01-09
Hello,

is there anyway to add the IList interface to the DictionaryBase?

A little background first.  I am attempting to set the datasource of a datagrid to a collection that inherits from the DictionaryBase class.  Turns out that for the DataSource property to accept a component, that component must implement the IList interface.  

The snag I run into is that I do not know how to write the Add method of the IList interface.

The header of my collection class looks like this:

public class Employees : DictionaryBase, IList

Is it possible to write an additonal Add method for the ILIst?  If so, could someone please provide some sample code.

thanks

0
Question by:brdrok
    11 Comments
     
    LVL 7

    Expert Comment

    by:NipNFriar_Tuck
    Try this... If course if you want to return the index of the key instead of the value you would change Values with Keys...

            public int Add( DictionaryEntry value )  {
                this.Add( value.Key, value.Value );
                return( this.IndexOf( value.Value ) );
            }

            public int IndexOf( String value )  {
                int i = 0;
                while ( i < Dictionary.Values.Count ) {
                    if ( value.Equals( Dictionary.Values[i] ) {
                        break;
                    }
                    i++;
                }
                return i;
            }
    0
     
    LVL 7

    Author Comment

    by:brdrok
    Hey Nip,

    when attempting to compile, the line complains at the follwoing line:

    if ( value.Equals( Dictionary.Values[i] ) {   <------throws an error

    The error message is below:

    Cannot apply indexing with [] to an expression of type 'System.Collections.ICollection'
         
    any suggestions?

    also, I have cut and pasted some of the code.  hopefully it will provide some clues.

    foreach (System.Data.DataRow dr in myDataSet.Tables[0].Rows)
    {
          Employee myEmployee = new Employee();
          myEmployee.EmployeeID = dr[0].ToString();
          myEmployee.FirstName = dr[1].ToString();
          myEmployee.LastName = dr[2].ToString();
          myEmployee.EmpJobTitle = Person.JobTitleObject( dr[3].ToString() );
          this.Add( myEmployee.EmployeeID, myEmployee );  
    }

    public void Add(string EmpKey, Employee NewEmployee)
    {
          Dictionary.Add(EmpKey, NewEmployee);
    }

    0
     
    LVL 15

    Expert Comment

    by:Thogek
    > Is it possible to write an additonal Add method for the ILIst?

    It would likely look something like

        public int IList.Add( object value )

    and only be accessible if the variable containing the reference to an object of this class was of type IList.

    IList.Add: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemcollectionsilistclassaddtopic.asp
    Explicit interface member implementations: http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_13_4_1.asp
    0
     
    LVL 7

    Expert Comment

    by:NipNFriar_Tuck
    There is a missing closing paren in the if condition...

    if ( value.Equals( Dictionary.Values[i] ) {

    should be

    if ( value.Equals( Dictionary.Values[i] ) ) {

    You may also need to add a ToString to the Dictionary.Values[i] so the line would look like...

    if ( value.Equals( Dictionary.Values[i].ToString() ) ) {

    Try this and see if it clears the error...
    0
     
    LVL 7

    Author Comment

    by:brdrok
    Hey NipNFriar,

    can't seem to get over that hump, so I figure I change it a little to the following:

    int i = 0;
    IEnumerator myEnumerator = this.GetEnumerator();
    while ( myEnumerator.MoveNext() )
    if ( value.Equals( myEnumerator.MoveNext() ) )
    {
          break;
    }
    i++;
    return i;

    dunno if this did anything other than got rid of the error message, but, when setting my dataSource property to:

    dataGrid1.DataSource = myEmployees;

    the datagrid displays four rows with empty columns which is not all bad because I have four Employee objects in my Employees collection.

    I suppose the reason why all the columns are empty is because I'll need to write an implementation for the indexer for the IList.  Any suggestions how to go about this?  Will be more than happy to add points, if requested.

    thanks

    0
     
    LVL 7

    Accepted Solution

    by:
    The short answer would be to do a read only indexer and return the values... i.e.

    public String this[ int index ]  {
          get  {
             return( (String) Dictionary.Values[index] );
          }
    }

    Of course you should do a bounds check on the index... But I believe this will work...
    0
     
    LVL 7

    Author Comment

    by:brdrok
    Hey NipNFriar,

    thanks for all your input.  Appearantly I am too dumb to figure this out.  Perhaps biting off more than I can chew.  Anyways....thanks for all your input and maybe in due time it will become more appearant to me, god willing :)

    0
     
    LVL 7

    Expert Comment

    by:NipNFriar_Tuck
    Did the indexer I give you solve you problem?  Also, you can not be "too dumb" since you found a place to ask enough questions (hopefully) to get you where you were going.
    0
     
    LVL 7

    Author Comment

    by:brdrok
    Yeah....been asking tons of questions these days.  figure i can move on to the next problem faster.  

    regarding the indexer...keep getting the "can't apply indexer to ICollection".  tried to get around it using the genumerator...got me there halfway but not all the way.  understand the underlying principle of interfaces but making it actually work in a program is a whole different story altogether.  

    0
     
    LVL 7

    Expert Comment

    by:NipNFriar_Tuck
    Try this then...

    public String this[ int index ]  {
          get  {
             String sRet = string.Empty;
             if ( ( index >= 0 ) && ( index < this.Count ) ) {
                 IEnumerator myEnumerator = this.GetEnumerator();
                 while ( myEnumerator.MoveNext() )
                       if ( index == 0 ) {

                       }
             
             }
             return( (String) Dictionary.Values[index] );
          }
    }

    nt i = 0;
    IEnumerator myEnumerator = this.GetEnumerator();
    while ( myEnumerator.MoveNext() )
    if ( value.Equals( myEnumerator.MoveNext() ) )
    {
         break;
    }
    i++;
    return i;
    0
     
    LVL 7

    Expert Comment

    by:NipNFriar_Tuck
    Grrr.... Sorry was still typing and hit tab then enter... this is what I was trying to type...


    public String this[ int index ]  {
          get  {
             String sRet = string.Empty;
             if ( ( index >= 0 ) && ( index < this.Count ) ) {
                 IEnumerator myEnumerator = this.GetEnumerator();
                 while ( myEnumerator.MoveNext() )
                     if ( index == 0 ) {
                         sRet = myEnumerator.Current.Value;
                         break;
                     }
                 }
             }
             return sRet;
          }
    }

    Also, you may want to check your IndexOf function...
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now