我试图从结构中获取值,并使用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;
}
答案 0 :(得分:0)
我想您必须更改:
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
分配添加到列表中的 last 元素,而不是全新一个