i have code to save a file like
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Text files|*.txt";
SaveDialog:
if ((bool)dialog.ShowDialog()) {
if (System.IO.Path.GetExtension(dialog.FileName) != ".txt") {
MessageBox.Show("You must select a .txt file");
dialog.FileName = "";
goto SaveDialog;
}
File.WriteAllText(dialog.FileName, txtEditor.Text);
}
i read that i should not use goto. i could use do/while and check that a valid extension was selected but that will add alot of unnecessary code. i find this neater. or is there a better/more correct way?
1 Answer