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

Home Posts Topics Members FAQ

Using foreach

Hello

I use foreach when I have a container of some kind for example an array or
ArrayList.

But in this case is it really possible to use foreach. In case it is how
would I write it.

for( int i = 0; i < dSMspCompositio n.Tables[0].Rows.Count; i++ )
compositionList .Add(dSMspCompo sition.Tables[0].Rows[i]["COMP_NAME"]);
//Tony
Apr 6 '06 #1
4 1487

you can use foreach over the datarow collection:

foreach (DataRow row in dSMspCompositio n.Tables[0].Rows)
{
compositionList .Add(row["COMP_NAME"]);
}

*** Sent via Developersdex http://www.developersdex.com ***
Apr 6 '06 #2
Tony,

You can use foreach on any object that supports IEnumerable interface or has
instance GetEnumerator method declared.
--

Stoitcho Goutsev (100)

"tony" <jo************ *****@telia.com > wrote in message
news:uT******** ******@TK2MSFTN GP04.phx.gbl...
Hello

I use foreach when I have a container of some kind for example an array or
ArrayList.

But in this case is it really possible to use foreach. In case it is how
would I write it.

for( int i = 0; i < dSMspCompositio n.Tables[0].Rows.Count; i++ )
compositionList .Add(dSMspCompo sition.Tables[0].Rows[i]["COMP_NAME"]);
//Tony

Apr 7 '06 #3

Stoitcho Goutsev wrote:
You can use foreach on any object that supports IEnumerable interface or
has instance GetEnumerator method declared.


BTW I wonder why ArraySegment<T> doesn't have a GetEnumerator<T >.

Is there an easy way of foreach'ing an ArraySegment<T> or a T[] array?

Apr 12 '06 #4
Ole Nielsby <ol*********@sn ailmail.dk> wrote:

Stoitcho Goutsev wrote:
You can use foreach on any object that supports IEnumerable interface or
has instance GetEnumerator method declared.
BTW I wonder why ArraySegment<T> doesn't have a GetEnumerator<T >.


Not sure. That's a bit odd.
Is there an easy way of foreach'ing an ArraySegment<T> or a T[] array?


You can certainly foreach over T[]. Here's an example:

using System;

class Foo<T>
{
T[] x;

public Foo(T[] data)
{
x = data;
foreach (T t in x)
{
Console.WriteLi ne(t);
}
}
}

class Test
{
static void Main()
{
new Foo<string>(new string[]{"hello", "there"});
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1907
by: Christian Decker | last post by:
I wanted to know if is a good idea to create classes that mirror exactly the structure of a database. This might sound a bit fuzzy but I'l explain myself with an example: CREATE TABLE `pl_cities` ( `city_id` int(10) NOT NULL auto_increment, `city_name` varchar(16) NOT NULL default '', `city_coordinates` varchar(10) NOT NULL default '', `city_country` enum('germany','switzerlan','austria') NOT NULL
11
2507
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
13
12992
by: inpuarg | last post by:
Is it possible to get all controls and all of their children on a Windows form without using recursive methods ?
3
33374
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current code is here: foreach ( string propertyName in ht.Keys ) { this.setProperty( propertyName, ht );
1
3444
by: Doug | last post by:
Hi, I have a collection of objects and I need to compare each item in the collection to see if it's a match on any or all of the others. I have stored my collection in an item like so: "List<PossibleDupsdups" where PossibleDups is an object of my own creation. I have been looking at how the ForEach and FindAll methods work and here is what I was thinking might be a good approach to doing this (in pseudocode):
9
5620
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return String.Format("'{0}'", value) End Function Basically, I just want to surround each value with single quotes. However, I am having trouble getting this to work using the System.Array.ForEach method. This may be partially because I am not very...
3
2687
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
6
1574
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. Now If I want to implement functionallity so I can compare animal with each other or with other animal for example compare a Chicken with a Cow in some way or a Cow with another Cow
5
2005
by: =?Utf-8?B?U2llZ2ZyaWVkIEhlaW50emU=?= | last post by:
Can someone help me convert this to the latest C# syntax using predicate/delegate? You can see my attempt in the comments. Also: how would I set a new font for q_ul? q_ul is a WPF TextBlock and I want to change its font. //q_ul.FontFamily.FamilyTypefaces.All<System.Collections.Generic.KeyValuePair<System.Windows.Markup.XmlLanguage,string>>(System.Collections.Generic.KeyValuePair<System.Windows.Markup.XmlLanguage,string> ftf, bool...
28
3197
by: natarajmtech | last post by:
Hi all, I have defined some array variables a ***************************************** @L=('CTT','CTC','CTA','CTG','TTA','TTG'); @S=('TCT','TCC','TCA','TCG','AGT','AGC'); @R=('CGT','CGC','CGA','CGG','AGA','AGG');
0
9728
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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
10389
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...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9205
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...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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
3867
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.