我需要一些帮助构建流畅的界面(类包装器),它允许我从数据库中的曲线集(在myWell中)创建(并命名)2D双重数组。理想的情况下,执行下面的语法(或类似的东西),当用户可以创建一个二维double数组:数据列表的类包装
Double [,] CurveDoubleName = CreateIPmathCurves.CreateCurve(comboBox1.Text).CurveName("NPHIL").Make()
凡comboBox1的数据来自一个名为frmMain WindowsForm。这是我到目前为止:
namespace UserProgram
{
public class CreateIPmathCurve : frmMain, ICanNameCurve, ICanLocateCurve, ICanMakeCurve
{
private string _comboBoxName;
private string _CurveName;
private string _data;
// Private constructor - Only allow instantiated functions to create an IP math Curve
private CreateIPmathCurve()
{ }
// Instantiating functions
public static ICanNameCurve CreateCurve()
{
return new CreateIPmathCurve();
}
// Chaining functions
private CreateIPmathCurve(string data) { _data = data; } //private constructor prevents instantiation of your builder
public ICanLocateCurve SetCurveName(string CurveName)
{
_CurveName = CurveName;
return this; // this allows chaining.
}
public ICanMakeCurve SetComboBoxName(string comboBoxName)
{
_comboBoxName = comboBoxName;
return this; // this allows chaining.
}
// ending functions
public DataObject CreateCurveDoublArrayObj(string comboBoxName)
{
try
{
// User will need to populate comboBox.. example: comboBox1.text
char[] delimiter = { ':' };
Curve1inText = comboBoxName;
string[] crvIn1 = Curve1inText.Split(delimiter);
string CurveSet = crvIn1[0];
string Curve = crvIn1[1];
ICurveSet InCurveSet = myWell.FindCurveSet(CurveSet);
ICurve InMyCurve = myWell.FindCurve(Curve);
if (InMyCurve == null)
{
MessageBox.Show("You need to input a curve");
}
ILogReading[] In = InMyCurve.LogReadings.ToArray();
double[,] CurveDouble = new double[In.Length, 2];
int j = 0;
foreach (ILogReading reading in InMyCurve.LogReadings)
{
CurveDouble[j, 0] = reading.Depth;
CurveDouble[j, 1] = reading.Value;
j++;
}
return new DataObject(CurveDouble);
}
catch (Exception ex)
{
MessageBox.Show("Error building Double Array\n" + ex.Message + "\n" + ex.StackTrace);
return null;
}
}
public double[,] MakeCurve()
{
//this is where CurveName input ("NPHIL") should name the 2D double array listed from dataobject.
// I receive a "non-invocable member 'DataObject' cannot be used like a method" error....
_CurveName = DataObject(_comboBoxName);
return this;
// I also receive a "cannot implicity convert type UserProgram.CreateIPmathCurve' to 'double [*,*]"...
}
}
// using interfaces to enforce Fluent Interface grammar -- can't make curve if you don't know the location of curve, etc...
public interface ICanNameCurve
{
ICanLocateCurve SetCurveName(string CurveName);
}
public interface ICanLocateCurve
{
ICanMakeCurve SetComboBoxName(string comboBoxName);
}
public interface ICanMakeCurve
{
DataObject CreateCurveDoublArrayObj(string comboBoxName);
}
}
你能帮我创建一个允许用户命名数据对象的CurveName()函数吗? (我尝试了一个getter-setter访问器,但我认为我没有正确地做,或者对它的工作原理缺乏强烈的概念性理解)
最后,你还可以帮我创建一个Make()函数把所有这些放在一起?
这被称为[*流畅界面*](https://en.wikipedia.org/wiki/Fluent_interface)。对于初学者来说可能会有点太高级了。为什么不从一般的方法/属性开始? – Sinatr
你想达到什么结果?双数组只包含双精度值。所以没有办法命名或找到你的曲线。 – Iqon
DataObject CreateCurveDoubleArray从我的井数据库(使用comboBox输入)找到曲线并填充名为CurveDouble ....的2D双重数组....我想将这个新的2D双重数组分配给名称(由CurveName指定)。我希望语法简单些,类似于上面的语法(CreateIPmathCurves.CreateCurve(comboBox1.Text).CurveName(“NPHIL”)。Make()) – endlessforms