title: C#: How to use indexable properties inside another class?
body: <p>I'm reading data from a view to items in a list. Each item is a string and three doubles:</p>
  
  <pre><code>public class StatReportDataItems
  {
      public string Label { get; set; }
  
      public double NumData1 { get; set; }
  
      public double NumData2 { get; set; }
  
      public double NumData3 { get; set; }
  }
  </code></pre>
  
  <p>Here is the class containing that list:</p>
  
  <pre><code>public class StatReportDataModel
  {
      public string SReportType { get; set; }
  
      public int numdatas { get; set; }
  
      public List&lt;StatReportDataItems&gt; StatReportDataList { get; set; }
  
      public string ReportTitle { get; set; }
  
  }
  </code></pre>
  
  <p>One of the many routines reading data to the list:</p>
  
  <pre><code>    public List&lt;StatReportDataItems&gt; GetReport888()
      {
          var query = from rv in ProcessDB.Report888Views
                      select new StatReportDataItems
                      {
                          Label = rv.Label,
                          NumData1 = (double)rv.NumData1
                      };
          var retValue = query.ToList();
          return retValue;
      } // GetReport888
  </code></pre>
  
  <p>Some routines also fill data into NumData2 and Numdata3 and then there is a general routine that displays the data using chart.js.</p>
  
  <p>Originally it was just "displaying", but now there are more and more things that should be done to the data before the general display routine gets to it. Since this includes looping through the data, it would make the code much better if the three Numdata's instead were an array/list/collection with three items (and in the future potentially more).</p>
  
  <p>Since returning an array is seen as a big no-no I thought I should use indexed properties. This would be a start:</p>
  
  <pre><code>public class Constants
  {
      public static int AntalData = 3;
  }
  
  public class NumDataType
  {
      private double[] numarr = new double[Constants.AntalData];
      public int Length
      {
          get { return numarr.Length; }
      }
      public double this[int index]
      {
          get { return numarr[index]; }
          set { numarr[index] = value; }
      }
  }
  </code></pre>
  
  <p>It would be easy to use this NumDataType directly in my application:</p>
  
  <pre><code>NumDataType NumItems = new NumDataType();
  NumItems[0] = 3.14;
  </code></pre>
  
  <p>However, when I try to use it in my class StatReportDataItems and then replace the</p>
  
  <pre><code>NumData1 = (double)rv.NumData1
  </code></pre>
  
  <p>in GetReport888 I can't get it to work, trying diffferent ways of writing the code. Most often I get compiler error "The name 'NumData' does not exist in the current context"</p>
  
  <p>The class StatReportDataItems with the changed NumData:</p>
  
  <pre><code>public class StatReportDataItems
  {
      public string Label { get; set; }
  
      public NumDataType NumData { get; } = new NumDataType();
  
  }
  </code></pre>
  
  <p>So, what am I doing wrong? Maybe it's just time for me to go home now and I will see it clearer tomorrow, but I would think some help is a safer bet...</p>
  
  <p>Since some commenters seem to have had problems reading what I've written I try to clarify here:</p>
  
  <ul>
  <li>An object of class StatReportDataModel is passed to a general chart presenting routine (code in knockout javascript, calling chart.js).</li>
  <li>The List StatReportDataList is filled with objects of class StatReportDataItems, this data is read from a DB view.</li>
  <li>The routine GetReport888 is one of many small routines responsible to get data, usually for one report each. It returns only one numerical data per item, some other return two or three.</li>
  </ul>
  
  <p>Everything works fine, but I can see that changing Numdata1 - Numdata3 into a NumData[] with three items would simplify the general js code. So I thought I should do this seemingly simple change. It's only in two places...</p>
  
  <p>So I created the NumDataType and replaced the three double properties in StatReportDataItems with one property of that type. And then I changed GetReport888 into:</p>
  
  <pre><code>    public List&lt;StatReportDataItems&gt; GetReport888()
      {
          var query = from rv in ProcessDB.Report888Views
                      select new StatReportDataItems
                      {
                          Label = rv.Label,
                          NumData[0] = (double)rv.NumData1
                      };
          var retValue = query.ToList();
          return retValue;
      } // GetReport888
  </code></pre>
  
  <p>I was prepared to possibly get some error message of the type "Cannot implicity convert..." if I had made something wrong, not that "The name 'NumData' does not exist in the current context"...</p>
  
  <p>This is the Report888View class, generated from the DB view:</p>
  
  <pre><code>[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Report888View")]
  public partial class Report888View
  {   
      private string _Label;      
      private System.Nullable&lt;double&gt; _NumData1;      
      public Report888View()
      {
      }
  
      [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Label", DbType="VarChar(100)")]
      public string Label
      {
          get
          {
              return this._Label;
          }
          set
          {
              if ((this._Label != value))
              {
                  this._Label = value;
              }
          }
      }
  
      [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_NumData1", DbType="Float")]
      public System.Nullable&lt;double&gt; NumData1
      {
          get
          {
              return this._NumData1;
          }
          set
          {
              if ((this._NumData1 != value))
              {
                  this._NumData1 = value;
              }
          }
      }
  }
  </code></pre>
  
  <p>So, why do I get this particular error?</p>
  
  <p>Edit: With the help of @howcheng I have the final GetReport888:</p>
  
  <pre><code>public List&lt;StatReportDataItems&gt; GetReport888()
  {
      List&lt;StatReportDataItems&gt; retValue = new List&lt;StatReportDataItems&gt;(); 
      fullmaktnodenProcessDB.Report888Views.ToList().ForEach(rv =&gt;
      { 
          StatReportDataItems data = new StatReportDataItems
          {
              Label = rv.Label,
              NumData = new List&lt;double&gt; {
                  (double)(rv.NumData1 == null ? 0.0 : rv.NumData1)
              },
          };
          retValue.Add(data);
      });
      return retValue;
  }
  </code></pre>