Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Enumerators in C# don't do what you think they do. They're used to go over collections of items, like arrays, lists or other objects that implement the <code>IEnumerable</code> interface, either explicitly or implicitly. They're not used to iterate over the fields of a struct. </p> <p>There are two ways of doing what you want. The first is to override the <code>ToString</code> method in your <code>ColumnSpec</code> struct. <code>ToString()</code> is a standard method shared by all types in .NET, and provides the print-out logic for your type. Since it's implemented inside your code, you can reference your fields explicitly and create a single string from them all.</p> <p>If you're looking for a generic solution after all, you'll have to look into <a href="http://msdn.microsoft.com/en-us/library/f7ykdhsy%28v=vs.110%29.aspx" rel="nofollow">Reflection</a>, which lets you examine the fields and properties of a struct at runtime, and has similar syntax to what you've tried:</p> <pre><code>ColumnSpec computed; // your instance. Type myType = typeof(ColumnSpec); The reflected Type of ColumnSpec. string combinedString = ""; foreach (FieldInfo field in myType.GetFields()) // this enumerates all public fields. { if (field.FieldType == typeof(string)) // only for strings { string fieldValue = field.GetValue(computed); // extract the value. combinedString += fieldValue; } } </code></pre> <p>This is a simplified example, but it should get you started with Reflection. A more comprehensive solution will have you drill down deeper, using reflection, into the array of <code>Column</code> objects, extracting the data from them as well.</p> <p>A third option, as mentioned in the comments by @Corak, will allow you to expose your values as an enumerator, maintaining their order, but without reflection, by adding the following method to your struct:</p> <pre><code>public IEnumerable&lt;string&gt; GetElements() { yield return specName; yield return delimiters; . . . // you get the idea. foreach (Column column in Columns) { yield return column.Name; } yield return testrangefile.Filename; } </code></pre> <p>This will expose an IEnumerable of strings, while explicitly <a href="http://msdn.microsoft.com/en-us/library/vstudio/9k7k7cf0.aspx" rel="nofollow"><code>yield</code>ing</a> allows you to fill this enumerable lazily, in the order you want. You can then call it like this:</p> <pre><code>foreach (string element in computed.GetElements()) { } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload