none
Help Understanding Code RRS feed

  • Question

  • Hiya - I have received a C# winform code file from another developer which works as we need it to, however I do not understand the code set-up.  

    For example - how is there one method that shares the same name of Save() and both methods accept parameters, but in one instance calling the method no parameter is passed?

    How is there a method that has the same name?  And how do you know which method to call when you call Save() with and w/o parameters?

    	private void STIMethod_Click(object sender, EventArgs e)
    	{
    		Save(LoadedPath);
    	}
    
    	private void STIA_Click(object sender, EventArgs e)
    	{
    		Save();
    	}
    	
    	
    	private void Save(string fileName, bool saveXmlOnly = true, bool calculated = false)
    	{
    		try
    		{
    			var xmlFileName = Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + ".xml";
    
    			if (!saveXmlOnly)
    			{
    				if (File.Exists(fileName)) { File.Delete(fileName); }
    
    				var i = 1;
    				if (!calculated && !MakeCalculations()) { return; }
    
    				foreach (var stage in SC)
    				{
    					string name;
    					//Do Stuff
    				}
    			}
    		}
    		catch { MessageBox.Show("An Error has occured."); 
    	}
    	private void Save(bool saveXmlOnly = true)
    	{
    		var save = new SaveFileDialog
    		{
    			InitialDirectory = @"C:\",
    			RestoreDirectory = true,
    			Title = "Select save location file name",
    			DefaultExt = saveXmlOnly ? "xml" : "xlsx"
    		};
    		
    		if (save.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(save.FileName))
    		{
    			Save(save.FileName, saveXmlOnly, !saveXmlOnly);
    		}
    	}

    Sunday, February 4, 2018 10:34 PM

Answers

All replies