Note that there are some explanatory texts on larger screens.

plurals
  1. POc# struct fields are "never assigned to" warnings
    primarykey
    data
    text
    <p>Based on <a href="http://alexreg.wordpress.com/2009/05/03/strongly-typed-csv-reader-in-c/" rel="nofollow noreferrer">http://alexreg.wordpress.com/2009/05/03/strongly-typed-csv-reader-in-c/</a>, I created a DLL which can read different file types. I also have unit tests that run successfully. I create a struct and use it as the generic type.</p> <p>Anyway, when I compile, I get a warning on each of the struct fields. For example: field 'FileReader.Tests.CsvReader.Record.Field1' is never assigned to, and will always have its default value 0</p> <p>I am in fact setting the value with SetValueDirect() and when I run through the tests or debug the code, I can verify that. Why is it giving me that error then, and how can I avoid or fix it?</p> <p>Here is some basic code to give you an idea. I'm guessing I haven't provided enough, but hopefully someone has a clue.</p> <pre><code>public abstract class FileReader&lt;TRecord&gt; : IDisposable where TRecord : struct { public TRecord? ReadRecord() { List&lt;string&gt; fields; string rawData; this.recordNumber++; while (this.ReadRecord(this.fieldTypeInfoList.Length, out fields, out rawData)) { try { // Insert the current record number to the beginning of the field list fields.Insert(0, this.recordNumber.ToString(CultureInfo.InvariantCulture)); // Convert each field to its correct type and set the value TRecord record = new TRecord(); FieldTypeInfo fieldTypeInfo; object fieldValue; // Loop through each field for (int i = 0; i &lt; this.fieldTypeInfoList.Length; i++) { fieldTypeInfo = this.fieldTypeInfoList[i]; bool allowNull = fieldTypeInfo.AllowNull == null ? this.AllowNull : fieldTypeInfo.AllowNull.Value; if (i &gt;= fields.Count &amp;&amp; !allowNull) { // There are no field values for the current field throw new ParseException("Field is missing", this.RecordNumber, fieldTypeInfo, rawData); } else { // Trim the field value bool trimSpaces = fieldTypeInfo.TrimSpaces == null ? this.TrimSpaces : fieldTypeInfo.TrimSpaces.Value; if (trimSpaces) { fields[i] = fields[i].Trim(); } if (fields[i].Length == 0 &amp;&amp; !allowNull) { throw new ParseException("Field is null", this.RecordNumber, fieldTypeInfo, rawData); } try { fieldValue = fieldTypeInfo.TypeConverter.ConvertFromString(fields[i]); } catch (Exception ex) { throw new ParseException("Could not convert field value", ex, this.RecordNumber, fieldTypeInfo, rawData); } fieldTypeInfo.FieldInfo.SetValueDirect(__makeref(record), fieldValue); } } return record; } catch (ParseException ex) { ParseErrorAction action = (ex.FieldTypeInfo.ParseError == null) ? DefaultParseErrorAction : ex.FieldTypeInfo.ParseError.Value; switch (action) { case ParseErrorAction.SkipRecord: continue; case ParseErrorAction.ThrowException: throw; case ParseErrorAction.RaiseEvent: throw new NotImplementedException("Events are not yet available", ex); default: throw new NotImplementedException("Unknown ParseErrorAction", ex); } } } return null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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