I need to have a method with parameters, but one of the parameters needs to have a default value, just in case it's not passed to the method when it's called. Here's my example:
This throws a Compiler Error CS0241. The online help documentation shows an example of a way to have a method with a default parameter by using overloading:
http://msdn.microsoft. com/en-us/library/294000kk(VS.90) .aspx
But I can't figure out what it's telling me to do. Basically I just want the string title to have a default value of Error! if the value doesn't get passed to the method, so I could call it like this:
error("Some error message");
And that would generate a messagebox with the default title of "Error!" and "Some error message" as the contents. Alternatively, I could call it like this:
error("Some error message", "Some title");
And I would get the same messagebox but the title would now be "Some title".
Is there a way I can do this?
Code:
private void error(string msg, string title = "Error!")
{
MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
http://msdn.microsoft. com/en-us/library/294000kk(VS.90) .aspx
But I can't figure out what it's telling me to do. Basically I just want the string title to have a default value of Error! if the value doesn't get passed to the method, so I could call it like this:
error("Some error message");
And that would generate a messagebox with the default title of "Error!" and "Some error message" as the contents. Alternatively, I could call it like this:
error("Some error message", "Some title");
And I would get the same messagebox but the title would now be "Some title".
Is there a way I can do this?
Comment