|
-
Aug 14th, 2012, 10:55 AM
#1
Thread Starter
Frenzied Member
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.}
*/
}
}
}
-
Aug 14th, 2012, 02:45 PM
#2
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:
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) { // ... } }
-
Aug 14th, 2012, 07:42 PM
#3
Thread Starter
Frenzied Member
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 );
}
}
}
-
Aug 19th, 2012, 09:06 PM
#4
Thread Starter
Frenzied Member
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.
-
Aug 24th, 2012, 02:59 AM
#5
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!
-
Aug 24th, 2012, 05:13 PM
#6
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|