C#: How to use indexable properties inside another class?

  • Thread starter Thread starter tommypeters
  • Start date Start date
T

tommypeters

Guest
(I tried asking this at Stackoverflow but people didn't seem to understand the question...)

I'm working on a statistics presentation routine. This routine is done as a general routine in konockout javascript, using the chart framework chart.js. The model is done in c# and it (usually) reads the data from a DB view so that the "work" is done on the database server and then presented in the same format, regardless of report, to the general routine.

  • An object of class StatReportDataModel is passed to a general chart presenting routine (code in knockout javascript, calling chart.js).
  • The List StatReportDataList is filled with objects of class StatReportDataItems, this data is read from a DB view.
  • 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.

I'm reading data from a DB view to items in a list. Each item is a string and three doubles:


public class StatReportDataItems
{
public string Label { get; set; }
public double NumData1 { get; set; }
public double NumData2 { get; set; }
public double NumData3 { get; set; }
}

Here is the class containing that list:


public class StatReportDataModel
{
public string SReportType { get; set; }
public int numdatas { get; set; }
public List<StatReportDataItems> StatReportDataList { get; set; }
public string ReportTitle { get; set; }
}


One of the many routines reading data to the list:


public List<StatReportDataItems> 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

Some routines also fill data into NumData2 and Numdata3 and then there is as mentioned a general routine that displays the data using chart.js.

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).

Since returning an array is seen as a big no-no ( CA1819: Properties should not return arrays ) I thought I should use indexed properties. This would be a start:


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; }
}
}


It would be easy to use this NumDataType directly in my application:


NumDataType NumItems = new NumDataType();
NumItems[0] = 3.14;


However, when I try to use it in my class StatReportDataItems and then replace the


NumData1 = (double)rv.NumData1

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"

The class StatReportDataItems with the changed NumData:


public class StatReportDataItems
{
public string Label { get; set; }
public NumDataType NumData { get; } = new NumDataType();
}

...and the new getReport888:

public List<StatReportDataItems> 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
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"...

So, why do I get this particular error? What did I do wrong? The first solution with the three (double) properties Numdata1 - Numdata3, mimicking the DB views, works fine. The change to NumData[0] seems small, but it seems like I have been blind to what error I have made...

Continue reading...
 
Back
Top