在这里遇到一些麻烦,希望有人可以提供帮助。我有一个下拉菜单,我可以从现场获取数据,只有当保存功能触发时,它不会保存,信息为空。我试图设置函数返回字符串,只是我接受一个int来显示选择号,所以它不兼容。有什么想法吗?是的,字符串是公开的。
public string GetDropSelection(int index)
{
Debug.Log(index);
switch (index)
{
case 1: BrakeWheelField="Front";
break;
case 2: BrakeWheelField="Rear";
break;
case 3: BrakeWheelField="Both";
break;
}
return BrakeWheelField; //I know this technique doesn't work because what is coming in is not what's going out.
Debug.Log("What is the selection?" + BrakeWheelField); //Variable has the value here.
}
public void GetBrakeData(string BrakeWheelField)
{
Debug.Log(BrakeWheelField); //By the time it gets to here, it's blank.
BrakeDateofPurchaseField=BrakeDateofPurchaseText.text;
BrakeMileageField=BrakeMileageText.text;
BrakeLocationField=BrakeLocationText.text;
BrakeLaborField=BrakeLaborText.text;
BrakeBrandField= BrakeBrandText.text;
BrakePriceField= BrakePriceText.text;
BrakeNotesField = BrakeNotesText.text;
DatabaseManager.Instance.SQLiteInit();
DatabaseManager.Instance.SaveBrakeInfo(BrakeDateofPurchaseField, BrakeMileageField, BrakeLocationField, BrakeLaborField, BrakeBrandField, BrakePriceField, BrakeWheelField, BrakeNotesField);
}
提前致谢!
答案 0 :(得分:0)
在您的switch语句中添加values
以查看您是否获得了1,2或3以外的索引 - 就像这样。
default
如果您希望默认为该答案,请将默认值添加到其他一种情况。如
switch (index)
{
case 1: BrakeWheelField="Front";
break;
case 2: BrakeWheelField="Rear";
break;
case 3: BrakeWheelField="Both";
break;
default:
Debug.Log("GroupDropSelection invalid index: "+index);
break;
}
单独回答,但我会将其保留在同一篇文章中。
switch (index)
{
default: // Make "Front" the default for other cases than 1,2or3
case 1: BrakeWheelField="Front";
break;
case 2: BrakeWheelField="Rear";
break;
case 3: BrakeWheelField="Both";
break;
}
中的 BrakeWheelField
不是您的全局变量,而是在GetBrakeData
的参数中传递的局部变量。如果您为GetBrakeData
提供空值,那么方法中的GetBrakeData
也是如此。在BrakeWheelField
中,如果您在Debug.Log
之前加BrakeWheelData
,您将获得全局值。看我的编辑:
this.