Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

IList and DictionaryBase

Posted on 2004-10-29
11
Medium Priority
?
1,034 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
Comment
Question by:brdrok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12447793
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
ID: 12448335
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
ID: 12448787
> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12463794
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
ID: 12465411
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:
NipNFriar_Tuck earned 1600 total points
ID: 12473831
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
ID: 12476975
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
ID: 12477307
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
ID: 12477504
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
ID: 12477771
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
ID: 12477813
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

618 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