Question

I have a struct like this and I have an array sorular[] soru.

public struct sorular
{
   public string resim;
   public string soru;
   public string seceneka;
   public string secenekb;
   public string secenekc;
   public string secenekd;
   public int dogrucevap;
   public int verilencevap;
}

And I would like to use a foreach loop, like:

foreach (int i in sorular.dogrucevap)
{

}

But I can't do it because of the cant get enumerator of dogrucevap error. I don't want to use a while or for loop :)

Was it helpful?

Solution

You can't iterate over something that is not a collection or otherwise iterable type.

Based on your updates, here is what I believe you are looking for:

foreach (sorular s in soru) {
    var i = s.dogrucevap;
    ...
}

OTHER TIPS

I have an array sorular[] soru.

Since have an array of these structs in soru, you can project out of them to get a set of the dogrucevap values.

IEnumerable<int> dogrucevaps = soru.Select(s => s.dogrucevap);

And then you may iterate these

foreach( int i in dogrucevaps )
{
   //TODO: use i
}

In condensed form

foreach(int i in soru.Select(s => s.dogrucevap))
{
   //TODO: use i    
}

In order to use sorular.dogrucevap in foreach statement, it should return object which has method GetEnumerator (or implement IEnumerable interface).

Why you have 'cant get enumerator of dogrucevap' error?

Foreach statement enumerates the elements of a collection, executing an embedded statement for each element of the collection and its defined as (C# Specification 8.8.4 The foreach statement):

foreach (local-variable-type identifier in expression)
    embedded-statement

If expression in foreach statement is not an array or dynamic, then it should have appropriate GetEnumerator method which defined as:

member on the type X with identifier GetEnumerator and no type arguments.

In your case expression returns value of type int. Thus int does not have GetEnumerator method, it cannot be used in foreach statement. Actually it does not make any sense, because int does not look as collection. What items you are expecting to enumerate in integer value 42?

How to fix problem? Assume you want to enumerate structs in your structs array. Then code should look like

 sorular[] soru;
 // intialize and fill array

 foreach(sorular s in soru)
 {
    // use s.dogrucevap
 }

Or if you want to enumerate dogrucevap values of structs in array, then you can project array items by selecting only this value:

 foreach(int dogrucevap in soru.Select(s => s.dogrucevap))
 {
     // use dogrucevap
 }

Your value is integer - you can't iterate integer directly.

If you just want foreach of single item - Enumerable.Repeat for single item to have one iteration:

foreach(int i in Enumerable.Repeat(questions.dogrucevap,1 ))
{

}

If you "dogrucevap" varialbe means something "from 1 to dogrucevap" you can construct Range out of it and foreach on the range:

foreach(int i in Enumerable.Range(1,questions.dogrucevap))
{

}

Firstly, instead of a struct you would be better off using a class.

As you are using an array sorular[] soru then you can do the following:

foreach (var item in soru)
{
    // do something with item.dogrucevap which is an int
}

Alternately, if you want to iterate over the fields of the structure you can use reflection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top