CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Question Dictonary Collection Issue

    can anyone reply to the following thread ? .any help would be highly appreciated .

    http://www.vbforums.com/showthread.p...llection-Issue

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Dictonary Collection Issue

    Sure, just post the question to this forum.

  3. #3
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Re: Dictonary Collection Issue

    i simple want to fill mcol data into the listbox .Why it says foreach statment cannot operate variable without GetEnumerator()? . let me know please .any help would be highly appreciated .

    Error 1 foreach statement cannot operate on variables of type 'Brands.BrandCol' because 'Brands.BrandCol' does not contain a public definition for 'GetEnumerator' F:\C#Progress\RPM\Brands\Brands\Properties\Form1.cs 44 15 Brands
    here is the following code what i have written .
    Code:
      namespace Brands{
        public partial class Form1 : Form{
           private BrandCol mcol = new BrandCol();
            public Form1()  {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e) {
                string strsql = null;            
                string strConn = Properties.Settings.Default.Path + "\\" + Properties.Settings.Default.DatabaseName;
                using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strConn))           {
                    conn.Open();
                    strsql = "Select * from Brand";
                    OleDbCommand cmd = new OleDbCommand(strsql,conn);                
                    cmd.CommandText = strsql;
                   OleDbDataReader reader = cmd.ExecuteReader();
                   if (reader.HasRows) {                  
                       while (reader.Read()) {
                           BrandCls Brand = new BrandCls();
                           Brand.Brandid = reader.GetByte(0);
                           Brand.BrandName = reader.GetString(1);
                           mcol.AddBrand(Brand);
                       
                                }  
        
                                    }
    
    
    
                }
                
            }
    
                public void FillListBox() {
                  foreach (BrandCls brand in mcol)
                      listBox1.Items.Add(brand.BrandName);		 
    	     
                    }    
    
    
                }
    
            
        }
    Last edited by firoz.raj; August 22nd, 2012 at 12:36 AM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Dictonary Collection Issue

    Post the code for the BrandCol class.

  5. #5
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Re: Dictonary Collection Issue

    here is the following code for Brandcol class .
    Code:
    
    namespace Brands{
        class BrandCol {
            // Declare a field to hold a dictionary of BrandCls objects keyed by the Brandid property (an integer)
            private Dictionary<int, BrandCls> _brandColl;
            // Constructor: instantiate an instance of the dictionary
            public BrandCol()   {
                _brandColl = new Dictionary<int, BrandCls>();
            }
    
            public void AddBrand(BrandCls brandClass)  {
                int key = brandClass.Brandid;
                if (_brandColl.ContainsKey(key)) {
                    _brandColl.Add(key,brandClass );
    
                }
    
                
               
            }
        }
    }

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Dictonary Collection Issue

    You need to derive the BrandCol class from IEnumerable.

    see http://msdn.microsoft.com/en-us/libr...numerable.aspx

  7. #7
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Re: Dictonary Collection Issue

    now i am getting datatype Issue.our brandID Datatype is byte in Access table.However i am getting the following error .
    Error 1 Cannot implicitly convert type 'System.Collections.Generic.Dictionary<byte,Brands.BrandCls>' to 'System.Collections.Generic.Dictionary<int,Brands.BrandCls>' F:\C#Progress\RPM\Brands\Brands\BrandCol.cs 14 26 Brands
    Error 2 Cannot convert type 'System.Collections.Generic.KeyValuePair<int,Brands.BrandCls>' to 'System.Collections.Generic.KeyValuePair<byte,Brands.BrandCls>' F:\C#Progress\RPM\Brands\Brands\BrandCol.cs 34 13 Brands

    Code:
    namespace Brands
    {
        class BrandCol : IEnumerable<BrandCls>
        {
            // Declare a field to hold a dictionary of BrandCls objects keyed by the Brandid property (an integer)
            private Dictionary<int, BrandCls> _brandColl;
            // Constructor: instantiate an instance of the dictionary
            public BrandCol()
            {
                _brandColl = new Dictionary<Byte,  BrandCls>();
            }
    
            public void AddBrand(BrandCls brandClass)
            {
                int key = brandClass.Brandid;
                if (_brandColl.ContainsKey(key))
                {
                    _brandColl.Add(key, brandClass);
    
                }
    
    
    
            }
    
            #region IEnumerable<BrandCls> Members
    
            public IEnumerator<BrandCls> GetEnumerator()
            {
                foreach (KeyValuePair<Byte , BrandCls> kvp in _brandColl)
                {
                    yield return kvp.Value;
                }
            }
    
            #endregion
    
            #region IEnumerable Members
    
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                throw new Exception("The method or operation is not implemented.");
            }
    
            #endregion
    
        }
    }
    Last edited by firoz.raj; August 24th, 2012 at 04:22 PM.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Dictonary Collection Issue

    Visual Studio is pretty good at pinpointing errors. All you have to do is double click on the error and it will take you to the offending line in the source code.

    As a new developer it's a bit tough to always understand what the error means, but once you figure it out, you'll remember it for the next time. I find it's helpful to type the error (in quotes) into a bing or google search and often times you'll find what you need to do to fix it.

    At any rate, here's your problem:

    Code:
    private Dictionary<int, BrandCls> _brandColl;
    
    public BrandCol()
    {
      _brandColl = new Dictionary<Byte,  BrandCls>();
    }
    Notice how the key types in red don't match? If you declare the type as int, then it needs to be int in the assignment statement. If it should be Byte, then it should be Byte everywhere.

    This is a general rule for all type safe variables (including generic collection classes like Dictionary<> and List<>): once you declare a variable as a certain type, you can't change that type when you assign it.

  9. #9
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Re: Dictonary Collection Issue

    Name:  BrandCol_Issue.jpg
Views: 109
Size:  73.7 KB

    Hi Arjay. why it says count=0 .after collecting so many property .
    Code:
     public void AddBrand(BrandCls brandClass)
            {
                Byte key = brandClass.Brandid;
                if (_brandColl.ContainsKey(key))
                {
                    _brandColl.Add(key, brandClass);
    
                }

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Dictonary Collection Issue

    Quote Originally Posted by firoz.raj View Post
    Hi Arjay. why it says count=0 .after collecting so many property .
    Try stepping through your code to find out.

  11. #11
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Re: Dictonary Collection Issue

    if brandcls is not added we need to add .so now it is ok .
    Code:
            public void AddBrand(BrandCls brandClass){
                Byte key = brandClass.Brandid;
                if (!_brandColl.ContainsKey(key)){
                    _brandColl.Add(key, brandClass);                
                   
                }
    another point .i simple want when user select any listview item from the list view. those specific brandname needs to come in text box.
    so can anyone tell me ? .how should i point selected brandname ? .and i want to store that brandname in the brand .finally i want to modify
    .after saving it needs to show in the listview .
    Last edited by firoz.raj; August 30th, 2012 at 02:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured