I want to remove my goto's in this code but I'm not sure how. There must be a cleaner way to write my code. Perhaps move the "if" statement to the end of each loop then use break?
Sorry if this is a stupid question, I'm self-taught, and I'm finishing up my first sizable program. Any suggestions as to how I can clean my code is welcomed, even though it isn't stated in my question.
Also this is just my beta release, so please forgive all the sillyness.
public void BtnRandomizeClick(object sender, EventArgs e)
{
Nowthatitssaved: //Goto marker -- Loops the saved/not saved stuff based on file dialog
switch (MessageBox.Show(@"Did you SAVE your edited list?", @"Just checking... It's important.",
MessageBoxButtons.YesNo))
{
#region Dialog Yes
case DialogResult.Yes:
if (FileChosen == null)
{
MessageBox.Show(
@"Please select a file to load before trying to randomize. You need some entries to randomize first eh?",
"Oh fiddlesticks...");
return;
}
dataGridAssociates.Rows.Clear();
var masterList = new List<Associate>();
var divertsList = new List<Associate>();
var mheList = new List<Associate>();
var loadingList = new List<Associate>();
var lines = File.ReadAllLines(FileChosen); //Array of all the lines in the text file
foreach (var assocStringer in lines)
{
if (assocStringer == null) continue;
var entries = assocStringer.Split('|');
var obj = (Associate) _bindingSource.AddNew();
if (obj == null) continue;
if (entries.Length < 7) break;
obj.FirstName = entries[0];
obj.LastName = entries[1];
obj.AssocId = entries[2];
obj.AssocRfid = entries[3];
obj.CanDoDiverts = Boolean.Parse(entries[4]);
obj.CanDoMhe = Boolean.Parse(entries[5]);
obj.CanDoLoading = Boolean.Parse(entries[6]);
obj.UniqueRandomID = Guid.NewGuid().ToString();
masterList.Add(obj);
}
var sortedMaster = masterList.OrderBy(d => d.UniqueRandomID).ToList();
var loaderCountRequested = 0;
var mheCountRequested = 0;
var divertCountRequested = 0;
var validDivertEntry = int.TryParse(txtDivertWorkers.Text, out divertCountRequested);
var validMHEEntry = int.TryParse(txtMHEWorkers.Text, out mheCountRequested);
var validLoaderEntry = int.TryParse(txtLoadersNeeded.Text, out loaderCountRequested);
//See if whomever used the inputs correctly
if (validDivertEntry == false)
{
MessageBox.Show(@"Please use an integer in the 'Divert Workers' textbox and try again");
return;
}
if (validMHEEntry == false)
{
MessageBox.Show(@"Please use an integer in the 'MHE Workers' textbox and try again");
return;
}
if (validLoaderEntry == false)
{
MessageBox.Show(@"Please use an integer in the 'Loaders' textbox and try again");
return;
}
var canDoDivertsQuery = sortedMaster.Where(a => a.CanDoDiverts);
var canDoMHEQuery = sortedMaster.Where(b => b.CanDoMhe);
var canDoLoadingQuery = sortedMaster.Where(c => c.CanDoLoading);
var loaders = 0;
var mhes = 0;
var diverters = 0;
//Begin the sorting method
foreach (Associate someAssocVar in canDoLoadingQuery)
{
if (loaders == loaderCountRequested)
goto MHEKTHX;
loadingList.Add(someAssocVar);
loaders++;
listBoxLoaders.Items.Add(loadingList[loaders - 1].FirstName + " " +
loadingList[loaders - 1].LastName);
}
MHEKTHX:
sortedMaster.RemoveAll(loadingList.Contains);
foreach (Associate someAssocVar in canDoMHEQuery)
{
if (mhes == mheCountRequested)
goto DivertsKTHX;
mheList.Add(someAssocVar);
mhes++;
listBoxMHE.Items.Add(mheList[mhes - 1].FirstName + " " +
mheList[mhes - 1].LastName);
}
DivertsKTHX:
sortedMaster.RemoveAll(mheList.Contains);
foreach (Associate someAssocVar in canDoDivertsQuery)
{
if (diverters == divertCountRequested)
goto DoneKTHX;
divertsList.Add(someAssocVar);
diverters++;
listBoxDiverts.Items.Add(divertsList[diverters - 1].FirstName + " " +
divertsList[diverters - 1].LastName);
}
DoneKTHX:
sortedMaster.RemoveAll(divertsList.Contains);
MessageBox.Show(@"You have " + sortedMaster.Count() + @" associates unassigned.");
break;
#endregion
#region Dialog No
case DialogResult.No:
if (MessageBox.Show(@"Would you like to save your work then?", @"It really does matter...",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
var saveDialog = new SaveFileDialog
{
InitialDirectory =
Convert.ToString(Environment.SpecialFolder.MyDocuments),
// ReSharper disable LocalizableElement
Filter = "Text (*.txt)|*.txt|All Files (*.*)|*.*",
// ReSharper restore LocalizableElement
FilterIndex = 1
};
if (saveDialog.ShowDialog() == DialogResult.OK)
{
FileChosen = saveDialog.FileName;
var assocToText = DatagridToString(dataGridAssociates, '|');
using (var outfile = new StreamWriter(saveDialog.FileName))
outfile.Write(assocToText);
}
goto Nowthatitssaved;
}
break;
#endregion
}
}
gotos can be replaced by a regularbreakfrom theforeachloop. I'm not posting this as an answer 'cause IMHO it does not really cover the topic fully (the code could probably use some refactoring/rewrite) and I can't really do much more from the phone ;) – Trust me - I'm a Doctor Jan 13 at 21:41