在oxygen.net中处理可空类型时出现奇怪的CLR错误



我在Oxygene .net上写一个程序我处理可空类型的方式似乎有问题

    namespace RULER;
interface
uses
  System,
  System.Drawing,
  System.Collections,
  System.Collections.Generic,
  System.Windows.Forms,
  System.ComponentModel;
type
  /// <summary>
  /// Summary description for Settings.
  /// </summary>
  Settings = public partial class(System.Windows.Forms.Form)
  private
      method Settings_Shown(sender: System.Object; e: System.EventArgs);
      method GetLowerBound : System.Nullable<Double>;
      method SetLowerBound(lowerbound:System.Nullable<Double>);
      method Settings_Load(sender: System.Object; e: System.EventArgs);
      method btnOK_Click(sender: System.Object; e: System.EventArgs);
  protected
      method Dispose(aDisposing: Boolean); override;
  public
      property LowerBound : System.Nullable<Double> read GetLowerBound write SetLowerBound;
    constructor;
  end;
implementation
{$REGION Construction and Disposition}
constructor Settings;
begin
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
method Settings.Dispose(aDisposing: Boolean);
begin
  if aDisposing then begin
    if assigned(components) then
      components.Dispose();
    //
    // TODO: Add custom disposition code here
    //
  end;
  inherited Dispose(aDisposing);
end;
{$ENDREGION}
method Settings.Settings_Shown(sender: System.Object; e: System.EventArgs);
begin
    LowerBound := System.Nullable<Double>(Controller.configuration.LowerBound);
end;
method Settings.GetLowerBound : System.Nullable<Double>;
begin
    var bound : Double;
    if Double.TryParse(txtLowerBound.Text, out bound) then
    begin
        Result := bound;
    end else
    begin
        Result := System.Nullable<Double>(nil);
    end;
end;
method Settings.SetLowerBound(lowerbound:System.Nullable<Double>);
begin
    if lowerbound.HasValue then
    begin
        txtLowerBound.Text := lowerbound.Value.ToString;
    end else
    begin
        txtLowerBound.Text := '';
    end;
end;
method Settings.Settings_Load(sender: System.Object; e: System.EventArgs);
begin
end;
method Settings.btnOK_Click(sender: System.Object; e: System.EventArgs);
begin
    var LB : System.Nullable<Double> := Self.GetLowerBound;
    if LB.HasValue then
    begin
        Controller.configuration.LowerBound := LB.Value;
    end;
end;
end.

当我点击触发btnOK_Click事件的按钮时,我得到一个奇怪的错误消息

运行时遇到致命错误。错误的地址在0x691886da,线程0x770上。错误码为0xc0000005。这错误可能是CLR中的错误,也可能是不安全或不可验证的错误部分用户代码。此错误的常见来源包括userCOM-interop或PInvoke的封送错误,这可能会损坏堆栈。

现在,我建议使用oxygen内置的"nullable"语言特性,因为它比System.Nullable更透明、更直观。例句:

method Settings.GetLowerBound : nullable Double;
begin
    var bound : Double;
    if Double.TryParse(txtLowerBound.Text, out bound) then
    begin
        Result := bound;
    end else
    begin
        Result := nil;
    end;
end;

无论如何,得到"运行时遇到致命错误"表示编译器错误。我将在我们这边记录您的测试用例的问题

这看起来是一个编译器错误。我已经转发了RemObjects的信息,以便在此上提交错误并修复它。同时,你也可以尝试更新的版本(或者测试版)。

我认为解决办法是替换

Result := System.Nullable<Double>(nil);

Result := new System.Nullable<Double>;

相关内容

  • 没有找到相关文章

最新更新