Question

Is it possible to set values for default parameters for PageSize in C#? For example:

public virtual void Render(string reportTitle, Rectangle pageSize = PageSize.A4)
{
    foreach (Page p in pages)
    {
        p.Render(document);
        document.NewPage();
        document.AddCreationDate();
        document.AddTitle(reportTitle);
        document.SetPageSize(pageSize);
    }
}

I have the following error in Visual Studio 2010:

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

Was it helpful?

Solution

When you write;

Rectangle pageSize = PageSize.A4

Your pageSize value can be change as a parameter.

From Named and Optional Arguments

A default value must be one of the following types of expressions:

  • 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.

The expression PageSize.A4 is not classified as a compile-time constant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top