目标:
我想为不经常更改的数据实现一个硬编码的查找表,但是当它确实发生更改时,我希望能够快速更新程序并重建。
计划:
我的计划是定义一个像这样的自定义数据类型......
private class ScalingData
{
public float mAmount;
public String mPurpose;
public int mPriority;
ScalingData(float fAmount, String strPurpose, int iPriority)
{
mAmount = fAmount;
mPurpose = strPurpose;
mPriority = iPriority;
}
}
然后,在主类中,像这样对数组进行硬编码......
public static ScalingData[] ScalingDataArray =
{
{1.01f, "Data point 1", 1},
{1.55f, "Data point 2", 2}
};
但是,这不会建立。我一直看到消息“ Type mismatch: cannot convert from float[] to ScalingData
”。
我怎样才能实现我的目标?
更新
到目前为止,我已尝试实施这些建议,但仍然遇到错误...
代码如下所示:
public class CustomConverter
{
//Lookup Table
private static ScalingData[] ScalingDataArray =
{
new ScalingData(1.01f, "Data point 1", 1),
new ScalingData(1.55f, "Data point 2", 2)
};
//Constructor
CustomConverter()
{
//does stuff
}
//Custom Data type
private class ScalingData
{
public float mAmount;
public String mPurpose;
public int mPriority;
ScalingData(float fAmount, String strPurpose, int iPriority)
{
mAmount = fAmount;
mPurpose = strPurpose;
mPriority = iPriority;
}
}
}
硬编码数组的错误是
No enclosing instance of type CustomConverter is accessible.
Must qualify the allocation with an enclosing instance of type CustomConverter
(e.g. x.new A() where x is an instance of CustomConverter).
编辑...根据下面的答案完成解决方案
public class CustomConverter
{
//Lookup Table
private static ScalingData[] ScalingDataArray =
{
new ScalingData(1.01f, "Data point 1", 1),
new ScalingData(1.55f, "Data point 2", 2)
};
//Constructor
CustomConverter()
{
//does stuff
}
//Custom Data type
private static class ScalingData
{
public float mAmount;
public String mPurpose;
public int mPriority;
ScalingData(float fAmount, String strPurpose, int iPriority)
{
mAmount = fAmount;
mPurpose = strPurpose;
mPriority = iPriority;
}
}
}