Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I´m new to C#.
I want to use foreach instead of for.

Coding with for:
C#
String[] properties = new String[] { "co", "extensionAttribute1", "mail" , "homeMDB"};
String[] result = new String[] {};

for (int i = 0; i < properties.Length; i++)
      {
         searcher.PropertiesToLoad.Add( properties[i] );
      }

SearchResult searchresult = searcher.FindOne();

for (int j = 0; j < properties.Length; j++)
            {
                if (searchresult.Properties.Contains(properties[j]))
                {
                    if (searchresult.Properties[properties[j]][0].ToString() != null)
                    {
                        result[j] = searchresult.Properties[properties[j]][0].ToString();
                    } 
            }



I tried to write it with foreach, first for sucessful, but now i don´t know how to fill the String[] result:

C#
foreach (String s in properties)
            {
                searcher.PropertiesToLoad.Add(s);
            }

foreach (String s in properties)
            {
                if (searchresult.Properties.Contains(s))
                {
                    if (searchresult.Properties[s][0].ToString() != null)
                    {
                        result[????]= searchresult.Properties[s][0].ToString();
                    }
                }
            }

Can anyone help me please?
Posted

If you want to use a foreach (and it makes some sense) and assuming there is enough space in the result array, then you just have to set up an integer of your own:
C#
int j = 0;
foreach (String s in properties)
            {
                if (searchresult.Properties.Contains(s))
                {
                    if (searchresult.Properties[s][0].ToString() != null)
                    {
                        result[j++]= searchresult.Properties[s][0].ToString();
                    }
                }
            }
Alternatively, I would set up result as a List of strings, and add to it each time:
C#
foreach (String s in properties)
            {
                if (searchresult.Properties.Contains(s))
                {
                    if (searchresult.Properties[s][0].ToString() != null)
                    {
                        result.Add( searchresult.Properties[s][0].ToString());
                    }
                }
            }
If you need an array afterwards, then List has a ToArray method. This way, you are not assigning space you don't need, or leaving null entries at the end of the array, or running out of array because you didn't assign enough elements.
 
Share this answer
 
Comments
Pravin Patil, Mumbai 28-Sep-11 4:38am    
My 5...
Just use a simple counter variable that is incremented every single time the foreach loop runs.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900