“不能隐式隐式浮点吗?浮动”

问题描述 投票:-1回答:2

我正在尝试减去2个浮点格式的变量(float xdiff = x1 - x2;),但出现错误“无法隐式隐式转换float?值d,c,s,m,radius,x1,y1,x2,y2从Windows表单输入中获取。

代码如下;

    public Nullable<float> d = null;

    public Nullable<float> c = null;

    public Nullable<float> s = null;

    public Nullable<float> m = null;

    public Nullable<float> radius = null;

    public Nullable<float> x1 = null;

    public Nullable<float> y1 = null;

    public Nullable<float> x2 = null;

    public Nullable<float> y2 = null;



private void Run_Click(object sender, EventArgs e)
        {
            d = float.Parse(this.downwind.Text);
            c = float.Parse(this.crosswind.Text);
            s = float.Parse(this.maxcross.Text);
            m = float.Parse(this.offset.Text);
            radius = float.Parse(this.rad.Text);
            x1 = float.Parse(this.x1coord.Text);
            y1 = float.Parse(this.y1coord.Text);
            x2 = float.Parse(this.x2coord.Text);
            y2 = float.Parse(this.y2coord.Text);

            float xdiff = x1 - x2;


        }
c# floating-point
2个回答
-1
投票

假设您处理了所有NULL情况,下面应该解决它。

float xdiff = (x1 - x2).Value;

0
投票

您应该使用

float? xdiff = x1 - x2;

或者最好是:

var xdiff = x1 - x2;
© www.soinside.com 2019 - 2024. All rights reserved.