|
-
Aug 22nd, 2012, 08:21 PM
#1
Thread Starter
Frenzied Member
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);
}
}
}
-
Aug 22nd, 2012, 08:25 PM
#2
Re: looping throgh foreach loop for collection.
What's BrandCol look like?
-tg
-
Aug 22nd, 2012, 08:31 PM
#3
Thread Starter
Frenzied Member
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 );
}
}
}
}
-
Aug 22nd, 2012, 08:32 PM
#4
Thread Starter
Frenzied Member
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 );
}
}
}
}
-
Aug 23rd, 2012, 12:23 AM
#5
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
-
Aug 23rd, 2012, 07:17 AM
#6
Thread Starter
Frenzied Member
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.
-
Aug 23rd, 2012, 08:09 AM
#7
Fanatic Member
Re: looping throgh foreach loop for collection.
 Originally Posted by firoz.raj
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
-
Aug 23rd, 2012, 08:24 AM
#8
Thread Starter
Frenzied Member
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
}
}
-
Aug 23rd, 2012, 08:33 AM
#9
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
-
Aug 23rd, 2012, 08:53 AM
#10
Thread Starter
Frenzied Member
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
}
}
-
Aug 23rd, 2012, 09:55 AM
#11
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
-
Aug 23rd, 2012, 10:30 AM
#12
Fanatic Member
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
-
Aug 24th, 2012, 11:13 PM
#13
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|