Results 1 to 13 of 13

Thread: looping throgh foreach loop for collection.

  1. #1
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Question looping throgh foreach loop for collection.

    can anyone tell me ? why i am getting the following error .when i tried to complile.
    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\Form1.cs 52 13 Brands
    Code:
    
    namespace Brands
    {
        public partial class Form1 : Form
        {
            private BrandCol mcol = new BrandCol();
            //  private Dictionary<BrandCol mcol>();
            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);
    
            }
    
           
    
    
    
    
    
            }
    
    
        }

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,655

    Re: looping throgh foreach loop for collection.

    What's BrandCol look like?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  3. #3
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Re: looping throgh foreach loop for collection.

    the following is a code for BrandCol
    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 );
    
                }
    
                
               
            }
        }
    }

  4. #4
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Re: looping throgh foreach loop for collection.

    the following is a code for BrandCol
    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 );
    
                }
    
                
               
            }
        }
    }

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,655

    Re: looping throgh foreach loop for collection.

    That's what I thought... you didn't inherit from an existing class that implements the IEnumerable interface (like Dictionary or List) ... nor did you implement the IEnumerable interface. Your class needs to do one or the other.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  6. #6
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Question Re: looping throgh foreach loop for collection.

    as i have seen some one .they just w click .and there is one option which say to implement .after clicking that automatcally the code for implement the IEnumerable interface come at the code window like the following . can you tell me ? How should i do it ? .any help would be highly appreciated .Thx in advance.
    Code:
      #region IEnumerable<BrandCls> Members
    
            public IEnumerator<BrandCls> GetEnumerator()
            {
                throw new Exception("The method or operation is not implemented.");
            }
    
            #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; Aug 23rd, 2012 at 07:45 AM.

  7. #7
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 04
    Location
    UK
    Posts
    671

    Re: looping throgh foreach loop for collection.

    Quote Originally Posted by firoz.raj View Post
    as i have seen some one .they just w click .and there is one option which say to implement .after clicking that automatcally the code for implement the IEnumerable interface come at the code window like the following . can you tell me ? How should i do it ? .any help would be highly appreciated .Thx in advance.
    Code:
      #region IEnumerable<BrandCls> Members
    
            public IEnumerator<BrandCls> GetEnumerator()
            {
                throw new Exception("The method or operation is not implemented.");
            }
    
            #endregion
    
            #region IEnumerable Members
    
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                throw new Exception("The method or operation is not implemented.");
            }
    
            #endregion
    
        }
    http://lmgtfy.com/?q=c%23+implementing+ienumerable

  8. #8
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Re: looping throgh foreach loop for collection.

    please don't give this type solution . i already tried at google .finally i post here .anyway i write the following code manually : IEnumerable<BrandCls> .just after in my brandcol class .class BrandCol after write click i got implement interface .and when i click i got the following code .but when i compile it gives no error this time .but when i run i got the following error.The method or operation is not implemented.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    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<int, 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()
            {
                throw new Exception("The method or operation is not implemented.");
            }
    
            #endregion
    
            #region IEnumerable Members
    
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                throw new Exception("The method or operation is not implemented.");
            }
    
            #endregion
    
        }
    }

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,655

    Re: looping throgh foreach loop for collection.

    I googled "How to implement IEnumerable interface in C#" ... and I got this
    http://support.microsoft.com/kb/322022
    and this
    http://codebetter.com/davidhayden/20...ustom-objects/
    and this
    http://www.codeproject.com/Articles/...merable-and-IE
    and this
    http://www.dotnetperls.com/ienumerable

    oh heck.... here's the results...
    https://www.google.com/search?q=how+...erface+in+c%23

    you're getting an error because you didn't add any code... you simply accepted the default code, which is to throw an exception... when you implement and interface, it's up to you to IMPLEMENT the interface... meaning adding code and all that...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  10. #10
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Re: looping throgh foreach loop for collection.

    why it says the following ?Error 1 Cannot convert type 'System.Collections.Generic.KeyValuePair<int,Brands.BrandCls>' to 'System.Collections.Generic.KeyValuePair<short,Brands.BrandCls>' F:\C#Progress\RPM\Brands\Brands\BrandCol.cs 34 13 Brands
    Code:
    using System;
    
    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<int, 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<Int16, 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
    
        }
    }

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,655

    Re: looping throgh foreach loop for collection.

    because an integer isn't an int16....
    Dictionary<int, BrandCls>
    vs
    KeyValuePair<Int16, BrandCls>

    try KeyValuePair<int, BrandCls> instead...

    int16 = short
    int32 = integer
    int64 = long


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  12. #12
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 04
    Location
    UK
    Posts
    671

    Re: looping throgh foreach loop for collection.

    From your questions it seems like you may need to brush up on your basics.

    http://www.csharp-station.com/Tutorial.aspx

  13. #13
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Re: looping throgh foreach loop for collection.

    i simple want to select the specific brand from the listview -> then deleting from listview -> from collection and finally i want to remove from the
    Brand Table .let me know please .any help would be highly appreciated .
    Code:
    private void btDelete_Click(object sender, EventArgs e){
                if (listView1.SelectedItems.Count > 0){                
                        foreach (BrandCls brand in _brandColl) {
                            listView1.Items.Remove(listView1.Items[brand].Text);
    
                        } 
                        
                    
                                            
                }
    
            
            }
    Last edited by firoz.raj; Aug 31st, 2012 at 10:15 AM.

Posting Permissions

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