can anyone reply to the following thread ? .any help would be highly appreciated .
http://www.vbforums.com/showthread.p...llection-Issue
Printable View
can anyone reply to the following thread ? .any help would be highly appreciated .
http://www.vbforums.com/showthread.p...llection-Issue
Sure, just post the question to this forum.
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);
}
}
}
Post the code for the BrandCol class.
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 );
}
}
}
}
You need to derive the BrandCol class from IEnumerable.
see http://msdn.microsoft.com/en-us/libr...numerable.aspx
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
}
}
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:
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.Code:private Dictionary<int, BrandCls> _brandColl;
public BrandCol()
{
_brandColl = new Dictionary<Byte, BrandCls>();
}
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.
Attachment 30221
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);
}
if brandcls is not added we need to add .so now it is ok .
another point .i simple want when user select any listview item from the list view. those specific brandname needs to come in text box.Code:public void AddBrand(BrandCls brandClass){
Byte key = brandClass.Brandid;
if (!_brandColl.ContainsKey(key)){
_brandColl.Add(key, brandClass);
}
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 .