Default Parameter for Value Must Be a Compile Time Constant

Default parameter for value must be a compile time constant?

DateTime.MinValue is not a const, because the language doesn't like const on DateTime. One option is to use DateTime? instead, i.e.

public static void DatesToPeriodConverter(DateTime start, DateTime? end = null,
out string date, out string time)
{
var effectiveEnd = end ?? DateTime.MinValue;
// ...
}

However, you will still have the issue of having non-default parameters after default parameters - you may need to re-order them to use that as a default.

Default Parameter must be a compile-time constant / int[] C#

From MSDN

"the default value must be one of the following :

a constant expression;

an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;

an expression of the form default(ValType), where ValType is a value type."

Arrays you created don't follow any of the above rulles

try this

public void C_Loader(int[] Index1=null, int[] Index2=null , int[] Index3=null)
{
if(Index1 ==null) Index1= new int[] {4,4,4};
if (Index2 == null) Index2 = new int[] { 8, 8, 8 };
if (Index3 == null) Index3 = new int[] { 10, 10, 10 };

.... your code
}

Default parameter must be compile-time constant

If you don't want to set it beforehand, I think you'll have to pass in a different set of defaults:

    public void UpdateTable(int month = -1, int year = -1)
{
if (month == -1) month = DateTime.Now.Month;
if (year == -1) year = DateTime.Now.Year;
}

Func as optional parameter of a function has to be a compile time constant?

Since default parameters must be compile time constants, you can't provide an easy default function quite like you want. You can work around this, though, using null as you pointed out.

private void func(Func<List<int>, bool> eval = null)
{
eval = eval ?? (_ => true);
// Do things.
}

This will assign your default implementation if null is passed, or the function is called without any parameters.

In modern versions of C#, you can use the null assignment operator, ??=.

This would look something like this:

eval ??= _ => true;

Default parameter value for 'postData' must be a compile-time constant

Why set a default value for it in the first place? This is not how functions work!

Use separate arguments and perform the string concatenation inside your function.

public string MakePostRequest(string url, string Username, string Password, string ForumUrl)
{
string postData = "username=" + Username + "&password=" + Password + "&remember=yes&submit=Login&action=do_login&url=" + ForumUrl + "member.php?action=login"
...
}

For a method with a name as generic as "MakePostRequest" having a default URL or default POST data sounds very strange.

To be honest, I'd expect it to accept just a URL and a mapping for the POST data and then it's up to the caller to passes the proper data for that request.

Default values in parameter

Default parameter for value must be a compile time constant. Dynamically calculated value is not accepted by compiler against optional parameter.

Let's say as anlternative DateTime? = null; and in method,

var effective_p_Date_Start = p_Date_Start ?? new DateTime(2006, 1, 1)

If necessary, you can follow up the way for others.

Optional parameters “must be a compile-time constant” with a const still bugging

The code you've given has two problems:

  • Optional parameters must come last (apart from params parameters)
  • const fields must be assigned compile-time constants, and string.Empty isn't a const field
  • Because Empty isn't a valid const, your default value isn't const. This is just a corollary of the second issue

Both of these are easily fixed:

private const string Empty = ""; // Literal rather than String.Empty
...

// Parameter name changed to be more readable and conventional
private string WriteFailedList(DeployResponse response, string prefix = Empty)
{
...
}

Or get rid of your own Empty constant:

private string WriteFailedList(DeployResponse response, string prefix = "")
{
...
}

(I'd also advise you to use camelCase for your parameters and local variables - so errorItems rather than ErrorItems. Or just errors, in fact. I'd also use a foreach loop instead of calling ToList() and then using ForEach.)

Default parameter must be compile-time constant in test class

First of all, ask your self what do you want to achieve with the test? Is it already tested?

Then if a test is dependent on other tests then you have done something wrong, so think again. A test should be isolated to its own context and not dependent on other tests.

Though, if you have a code snippet that is the same for the two tests then you should extract that to it's own function that the both tests can call. But have in mind a good test only test one thing.



Related Topics



Leave a reply



Submit