IList and DictionaryBase

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

LVL 7
brdrokAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
NipNFriar_TuckConnect With a Mentor Commented:
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
 
NipNFriar_TuckCommented:
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
 
brdrokAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ThogekCommented:
> 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
 
NipNFriar_TuckCommented:
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
 
brdrokAuthor Commented:
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
 
brdrokAuthor Commented:
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
 
NipNFriar_TuckCommented:
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
 
brdrokAuthor Commented:
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
 
NipNFriar_TuckCommented:
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
 
NipNFriar_TuckCommented:
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
All Courses

From novice to tech pro — start learning today.