Results 1 to 6 of 6

Thread: Dictonary Collection Issue

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Dictonary Collection Issue

    Hi, Friends, why i am getting the following errror ? .when we declare dictionary Collection? .
    let me know .any help would be highlly appreciated .
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Brands{
        class BrandCol {
            public BrandCol() {     
                BrandCol brandcoll=new Dictionary <BrandCls _brandclass>();
            /* public void Addbrand(BrandCls _brandclass){
             
              int key =_brandclass.Brandid ;
                 if (BrandCol.}
             
             
             */
             
             
        }
        }
    }

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Dictonary Collection Issue

    You need to supply two generic parameters to the Dictionary: _brandclass isn't a type name I'm guessing. Judging from the commented out code that follows, I'm guessing you want something like this:
    csharp Code:
    1. class BrandCol {
    2.     // Declare a field to hold a dictionary of BrandCls objects keyed by the Brandid property (an integer)
    3.     private Dictionary<int, BrandCls> _brandColl;
    4.     // Constructor: instantiate an instance of the dictionary
    5.     public BrandCol() {
    6.         _brandColl = new Dictionary<int, BrandCls>();
    7.     }
    8.    
    9.     public void AddBrand(BrandCls brandClass) {
    10.         // ...
    11.     }
    12. }

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Dictonary Collection Issue

    first i simple want to fill all the brand table record in the dataset .after that i want to fill all the dataset data
    into the _brandColl collection .
    Code:
    private void Form1_Load(object sender, EventArgs e) {
                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();
                    OleDbCommand cmd = new OleDbCommand("Select * from Brand");
    
                }
    here is the brandcol code .
    Code:
    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

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Dictonary Collection Issue

    first i simple want to fill all the brand table record in the dataset .after that i want to fill all the dataset data
    into the _brandColl collection .
    Code:
    private void Form1_Load(object sender, EventArgs e) {
                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();
                    OleDbCommand cmd = new OleDbCommand("Select * from Brand");
    
                }
    here is the brandcol code .
    Code:
    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 );
    
                }
    
                
               
            }
    }
    Last edited by firoz.raj; Aug 19th, 2012 at 09:11 PM.

  5. #5
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: Dictonary Collection Issue

    You need to add dataset.fill() in your form load event. When it's done, you have to loop through the dataset (row/column) properties and assign them
    to your class BrandCls that has properties the same with the fields returned by your query. After that, pass it to your AddBrandMethod().


    Cheers!
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Dictonary Collection Issue

    can anyone tell me ? why i am unable to fill all the brands in a collection .beause it still says count=0 .let me know .any help would be highly appreciated .
    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<Byte, BrandCls> _brandColl;
            // Constructor: instantiate an instance of the dictionary
            public BrandCol() {
                _brandColl = new Dictionary<Byte,  BrandCls>();
            }
    
            public bool AddBrand(BrandCls _brandClass){
                Byte key = _brandClass.Brandid;
                if (_brandColl.ContainsKey(key)){
                    _brandColl.Add(key, _brandClass);
                    return true;
    
                } else {
                  return false;
    
                  }
    
            }
    
            #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; Aug 31st, 2012 at 09:46 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
  •  



Click Here to Expand Forum to Full Width