How can I start the index in an ArrayList
at 1 instead of 0? Is there a way to do that directly in code?
(Note that I am asking for ArrayList
First, let me preface this answer by explaining my use case: Excel Interop. It's much easier to read and write data using arrays, but Excel Range.Value2 magically returns a 1 based object array.
So, if you are writing to Excel and that's the reason you are asking this question in the first place... then perhaps stop fighting c# and let Excel instantiate the array for you. So instead of:
object[,] newArray = new object[indexR, indexC];
You can use:
object[,] newArray = (object[,])RangeToWriteTo.Value2;
In my case, I created a wrapper class to allow me to use an array for the properties like so:
public abstract class ExcelRowBase
{
public object[,] data;
public ExcelRowBase(int index)
{
data = new object[2, index + 1];
}
}
public class InstanceRowModel : ExcelRowBase
{
public InstanceRowModel() : base(8)
{
//constructor unique to Wire Table
}
public object Configuration
{
get
{
return data[1, 1];
}
set
{
data[1, 1] = value;
}
}
...
So in all cases, I'm reading from the same index. So to make the transition from a model I'm passing into a Create method, I just need to copy over the properties into the model that uses the array created from Excel.
insertingModel.data = (object[,])writeRange.Value2;
//manually copying values from one array to the other
insertingModel.Configuration = model.Configuration;
...
writeRange.Value2 = insertingModel.data;
This works for me for now because I only have to do this in the create function. In update / get / delete, you're getting the Excel based array anyway. Perhaps a future improvement would be to create an Excel range factory that avoids the 0 based default construction all together, but this solution just gave me greens on my tests, so I'm moving on!