我有一些看起来像这样的代码:
int i = 0;
foreach (var line in theCP4UnknownList.Distinct())
{
var splitUnknowns = line.Split(' ');
i++;
unknownCP4Counter = unknownCP4Counter - i;
KTS_Save saveForm = new KTS_Save (splitUnknowns[0], splitUnknowns[1], splitUnknowns[3], splitUnknowns[4], openDataBase2File.FileName, unknownCP4Counter);
saveForm.ShowDialog();
}
基本上我要做的是显示有多少未知部分,并减少每个表格上的计数器。
因此,如果有10个部分,则为用户弹出的第一个表单将在右上角显示“10”。用户退出当前表单后,将弹出一个新表单并在右上角有“9”....这将继续发生,直到它达到0.这也是表单自那时起停止unknownCP4Counter
设置为theCP4UnknownList
中的项目数。
有谁知道我的代码出了什么问题?它似乎在每个表单上输出一个“0”。
private string _Name;
private string _PtpName;
private string _TapeWidth;
private string _FeederPitch;
private string _DataBaseFileName;
private int _Counter;
public KTS_Save()
{
InitializeComponent();
}
public KTS_Save(string Name, string PtpName, string TapeWidth, string FeederPitch, string DBFileName, int Counter)
{
InitializeComponent();
_Name = Name;
_PtpName = PtpName;
_TapeWidth = TapeWidth;
_FeederPitch = FeederPitch;
_DataBaseFileName = DBFileName;
nameTextBox.Text = _Name;
ptpNameTextBox.Text = _PtpName;
tapeWidthTextBox.Text = _TapeWidth;
feederPitchTextBox.Text = _FeederPitch;
counterLabel.Text = _Counter.ToString();
}
答案 0 :(得分:1)
通过
int i = 0;
int unknownCP4Counter = theCP4UnknownList.Distinct().Count; // 10
foreach (var line in theCP4UnknownList.Distinct())
{
i++;
unknownCP4Counter = unknownCP4Counter - i;
}
你正在做的事情:
unknownCP4Counter = 10;
// foreach starts
unknownCP4Counter = 9;
unknownCP4Counter = 7;
unknownCP4Counter = 4;
unknownCP4Counter = 0;
unknownCP4Counter = -5;
unknownCP4Counter = -11;
unknownCP4Counter = -18;
unknownCP4Counter = -26;
unknownCP4Counter = -35;
unknownCP4Counter = -45;
// foreach stops
如果你想创建一个递减计数器,我建议你改为:
int unknownCP4Counter = theCP4UnknownList.Distinct().Count; // 10
foreach (var line in theCP4UnknownList.Distinct())
{
unknownCP4Counter--;
}
编辑基于OP的编辑:
您忘记使用_Counter
参数初始化构造函数KTS_Save
中的Counter
字段。由于 int 不是可空类型,因此其默认值为0
。
答案 1 :(得分:0)
什么是unknownCP4Counter初始化为?它每次都被我贬低,如果它被初始化为总数,那么我将永远是总数 - 总数是0.也许你的意思是写:
unknownCP4Counter = i;