-
August 20th, 2012, 08:13 PM
#1
Dictonary Collection Issue
can anyone reply to the following thread ? .any help would be highly appreciated .
http://www.vbforums.com/showthread.p...llection-Issue
-
August 21st, 2012, 03:12 PM
#2
Re: Dictonary Collection Issue
Sure, just post the question to this forum.
-
August 21st, 2012, 06:33 PM
#3
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.
-
August 22nd, 2012, 01:52 PM
#4
Re: Dictonary Collection Issue
Post the code for the BrandCol class.
-
August 22nd, 2012, 06:45 PM
#5
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 );
}
}
}
}
-
August 24th, 2012, 03:30 PM
#6
Re: Dictonary Collection Issue
You need to derive the BrandCol class from IEnumerable.
see http://msdn.microsoft.com/en-us/libr...numerable.aspx
-
August 24th, 2012, 04:20 PM
#7
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.
-
August 24th, 2012, 08:58 PM
#8
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.
-
August 24th, 2012, 10:16 PM
#9
Re: Dictonary Collection Issue
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);
}
-
August 25th, 2012, 01:52 AM
#10
Re: Dictonary Collection Issue
Originally Posted by firoz.raj
Hi Arjay. why it says count=0 .after collecting so many property .
Try stepping through your code to find out.
-
August 30th, 2012, 02:46 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|