473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

default parameters in methods

HaLo2FrEeEk
404 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. private void error(string msg, string title = "Error!")
  2. {
  3.     MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
  4. }
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?
May 13 '10 #1
7 2591
ThatThatGuy
449 Expert 256MB
@HaLo2FrEeEk
You should compile this code in .net framework 4.0

default parameters are not supported, prior to framework 4.0
May 13 '10 #2
HaLo2FrEeEk
404 256MB
I'm trying to maintain compatibility with older systems that might not have the new framework installed. I found a way around it though. All I had to do was change my method to this:

Expand|Select|Wrap|Line Numbers
  1. private void error(string msg, string title)
  2.         {
  3.             if (string.IsNullOrEmpty(title))
  4.             {
  5.                 title = "Error!";
  6.             }
  7.             MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
  8.         }
And call it with this:

error("some message", null);

That worked perfectly.
May 13 '10 #3
ThatThatGuy
449 Expert 256MB
@HaLo2FrEeEk
So you could have anyways done it.... why did you tried the new one
May 13 '10 #4
HaLo2FrEeEk
404 256MB
Because I didn't think about that until after I made the post, and by the time I did it was too late to delete it.
May 13 '10 #5
GaryTexmo
1,501 Expert 1GB
I didn't know there was default params in .NET 4.0... cool!

Anyway, the proper way to do this previously was with overloads... for example:

Expand|Select|Wrap|Line Numbers
  1. public void MyMethod(int id, string fname, string lname) { ... }
  2.  
  3. public void MyMethod(int id, string fname)
  4. {
  5.   MyMethod(id, fname, "NOT ENTERED");
  6. }
  7.  
  8. public void MyMethod(int id)
  9. {
  10.   MyMethod(id, "NOT ENTERED", "NOT ENTERED");
  11. }
  12.  
  13. // etc...
}
May 13 '10 #6
tlhintoq
3,525 Expert 2GB
For earlier framework you have to send all parameters. There are no options. If you want an option to not send it then make an override without the second param that adds your default then calls the 2 param version

Void test(string one, string two)
{
}

void test(string one)
{
Test(one, "default");
}
May 13 '10 #7
HaLo2FrEeEk
404 256MB
My way works too, I send a null value and my method checks if the value is null and sets it to a default.
May 13 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
2
by: CMan | last post by:
Hi All, I am trying to use a COM component that has methods with default parameters. How can I call these methods from C#. Not all default values are in the documentation of the control. ...
2
by: Michael Stembera | last post by:
I would like to use default parameters in nested templates but MS VC++ 7.1 chokes on it. Does anyone know how to fix the simple example below or if indeed it is possible? template <int N=7>...
3
by: Schwarzbauer Günter | last post by:
Hello, Does the C++ standard allow default parameters when defining a typedef for a function type, e.g. typedef void (*TYPE_Report)(const char* message, const char* details = 0); This was...
2
by: Vic Y | last post by:
Hi, I am trying to call a user defined function(UDF) from a stored proc, using a default parameter in the called UDF (e.g. @bid_price_type int = 0 ). However the calling stored proc complains...
8
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of...
1
by: Sunil | last post by:
I have a com object i imported using tblimp. Iam creating an object using server.createobject. everything works fine in VB#. when using c# i discovered that there are default parameters which...
21
by: Dmitry Anikin | last post by:
I mean, it's very convenient when default parameters can be in any position, like def a_func(x = 2, y = 1, z): ... (that defaults must go last is really a C++ quirk which is needed for overload...
5
by: Christoph Haas | last post by:
Hi, list... I wondered if it's possible to use global (module) variables as default parameters. A simple working example: ---------------------------------------- #!/usr/bin/python ...
2
by: desktop | last post by:
Are there some way to mix default and non-default parameters in a constructor: class test { public: test(int i = 45, int j) : pp(j){} private: int pp; int i;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.