从结构中获取价值

埃维克·加扎里安(Evik Ghazarian)

我试图从结构中获取值,并使用OverSized中的值在if语句中使用它。但是该程序似乎并不从超大型程序中获取任何信息。通过在控制台上编写,我可以看到overSized的值是真实的(例如),但是在第三代码节中,沉重的值实际上没有任何值。我是否以错误的方式致电过大电话?这是我的包装箱结构:

public struct ShippingBox
{
    public int Length;
    public int Width;
    public int Height;
    public bool OverSized;
    public ShippingBox(
        int length,
        int width, 
        int height,
        bool overSized)
    {
        this.Length = length;
        this.Width = width;
        this.Height = height;
        this.OverSized = overSized;
    }
}

这就是我给struct赋值的方式:( typewiseboxes已经定义)

     bool overSized = false;
     if (some condition)
                    {
                        overSized = true;
                    }

                int l = 0;
                int w = 0;
                int h = 0;
                if (overSized)
                {
                    l = 100;
                    w = 100;
                    h = 100;
                }
                if (boxTypeWiseNumOfBoxes > 0)
                {
                    typeWiseBoxes.Add(
                        new ShippingBox(
                            l,
                            w,
                            h,
                            overSized));
                }

现在尝试通过以下方法获得超大尺寸的价值:

ShippingBox specialBox = new ShippingBox();
        var heavy = specialBox.OverSized; //also tried bool heavy
        int tempLength = 0;
        int tempWidth = 0;
        int tempHeight = 0;
        if (heavy)
        {
            tempLength = 101;
            tempWidth = 8;
            tempHeight = 12;
        }
        else if (!heavy)
        {
            tempLength = 77;
            tempWidth = 8;
            tempHeight = 12;
        }
显示名称

我想你必须改变:

ShippingBox specialBox = new ShippingBox(); // this assigns specialBox an instance of a brand new ShippingBox object
    var heavy = specialBox.OverSized; //heavy will be undefined
    ...

至:

ShippingBox specialBox = typeWiseBoxes.Last(); // this assigns specialBox the instance of the last ShippingBox object added to typeWiseBoxes List
    var heavy = specialBox.OverSized; // heavy is set to the value of OverSized property of last typeWiseBoxes List element
    ...

并由此赋予specialBox最后添加到列表中,而不是一个元素全新的一个

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章