473,796 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why this erroe is cming???foreach statment cannot operate on type object...in windows

46 New Member
I m trying to write an event for the CheckedListBox. ..here m using foreach loop
syntsx:
(type identifier in expression)

but it is giving error :foreach statment cannot operate on variables of type object.
because object does not contain a definattion for GetEnumerator or it is inaccessible

private void btnOK_Click(obj ect sender, System.EventArg s e)

{

string str = "You would like to visit:\n";

foreach (object item in clbStates.Check edItems)

{

str += item.ToString() + "\n";

}

MessageBox.Show (str);

}
Aug 29 '07 #1
4 1567
nateraaaa
663 Recognized Expert Contributor
I m trying to write an event for the CheckedListBox. ..here m using foreach loop
syntsx:
(type identifier in expression)

but it is giving error :foreach statment cannot operate on variables of type object.
because object does not contain a definattion for GetEnumerator or it is inaccessible

private void btnOK_Click(obj ect sender, System.EventArg s e)

{

string str = "You would like to visit:\n";

foreach (object item in clbStates.Check edItems)

{

str += item.ToString() + "\n";

}

MessageBox.Show (str);

}
Try changing your foreach loop to this.

foreach (string item in clbStates.Check edItems)
{
str += item.ToString() + "\n";
}

Nathan
Aug 29 '07 #2
neehakale
46 New Member
Not working still giving the same error...foreach not support object..though i have write string there...whats the probs not getting?
Aug 30 '07 #3
neehakale
46 New Member
Hey..i find out the soun for this..if anbody is facing the same error..this might be helpful..ths why i pasting my solun here

private void btnOk_Click(obj ect sender, System.EventArg s e)
{
string str="You would like to visit: \n";
for(int i=clbstates.Ite ms.Count-1;i>=0;i--)
{
if(clbstates.Ge tItemChecked(i) ==true)
{//In this case we are using check Boxes
//here with the help of GetItemChecked( Item to check)
//check whether the given item is checked or not.
//if the item is checked add it to the string variable str

str+=clbstates. Items[i].ToString()+ "\n";


}
}
MessageBox.Show (str);
}
Aug 30 '07 #4
Plater
7,872 Recognized Expert Expert
I had no problem using foreach so I don't know what you were doing wrong

Expand|Select|Wrap|Line Numbers
  1. CheckedListBox cb = new CheckedListBox();
  2. cb.Items.Add(1,true);
  3. cb.Items.Add(2);
  4. cb.Items.Add("fred");
  5. cb.Items.Add("barny",true);
  6. cb.Items.Add(DateTime.Now,true);
  7. foreach (object o in cb.CheckedItems)
  8. {
  9.    MessageBox.Show(o.GetType().Name);
  10. }
Aug 30 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

32
4157
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open Source" manner, and in an attempt to build a ground-swell of support to convince the folks at Microsoft to add it. Proposal: "first:" "last:" sections in a "foreach" block The problem: The foreach statement allows iterating over all the...
13
2002
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was neccessary in nearly every collection. But with generics I consider this feature of foreach as dangerous. Casting is now almost always unwanted. interface IFoo{} class Foo:IFoo{} class FooBar:IFoo{}
104
7205
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
32
6525
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains on the "bit = false;" stating that bit is read-only.
13
1860
by: Michael Brown | last post by:
Hi there. Can anybody comment on the efficiency of the following technique: foreach (SomeItem Item in SomeNativeDotNet.Collection) { // Whatever } My concern is that the "Collection" property itself may be invoked on each iteration rather than being called just once at the outset of "foreach". If so then there's no way to know whether the call to retrieve this property is
11
2506
by: Benny | last post by:
I just wanted to throw the discussion out there on what the best practice people feel is for using large objects in a foreach loop. For example if you are reusing an Image object in a loop like this (letters above snippets are for reference purposes): A foreach ( string s in myList ) { Image img = Image.FromFile( s ); // operate on img
6
13218
by: titan nyquist | last post by:
Can I step through a struct using foreach? I want a method that steps through a struct, and prints out the name/value of each member. Can it be done? The error I get when trying this is: "foreach statement cannot operate on variables of type '...' because '...' does not contain a public definition for 'GetEnumerator'. And it points me to http://msdn2.microsoft.com/en-us/library/t51esaeb(vs.80).aspx I guess this is the answer, but...
4
2567
by: Peter Morris | last post by:
I am not sure what you are asking. You seem to be asking how to implement a plain IEnumerable on a composite structure, but then your example shows a flat structure using "yield". Your subject makes me infer that you think foreach is enirely useless for composite structures. I will address the subject text, because that makes the most sense to me :-) Take the following class public class MyTreeNode : IEnumerable<MyTreeNode> {
2
10013
boss32178
by: boss32178 | last post by:
Ok I get this error when i try to run the web app. foreach statement cannot operte on variables of type 'object' does not contain a public definition for 'GetEnumerator' here is the code #region Number String public string MyNum(System.Object MyString) { string temp; foreach (string mylet in MyString)
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.