I have some code that looks like this:
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();
}
Basically what I am trying to do is display how many unknown parts there are and decrease the counter on each form.
So if there were 10 parts, the first form to pop up for the user would have a “10” in the upper right corner. After the user exits out of the current form, a new form will pop up and have “9” in the upper right corner…. This would continue to happen until it reached 0. Which is also when the forms would stop coming since the unknownCP4Counter is set to how many items are in theCP4UnknownList.
Does anyone know what is wrong with my code? It seems to be outputting a “0” on every form.
EDIT:
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();
}
By doing
you are doing:
If you want to create a decreasing counter, I suggest you to do instead:
EDIT based on OP’s edit:
You forgot to initialize the
_Counterfield in your constructorKTS_Savewith theCounterparameter. As int is not a nullable type, its default value will be0.