Beckam White
Beckam White

Reputation: 143

Custom default struct as optional parameter

I have a struct called WindowProperties. It has 3 properties, a static Default and a constructor with optional parameters.

public struct WindowProperties
{
    public string Title;
    public int Width;
    public int Height;

    public static WindowProperties Default => new WindowProperties("Fury Engine", 1280, 720);

    public WindowProperties(string title = "Fury Engine", int width = 1280, int height = 720)
    {
        Title = title;
        Width = width;
        Height = height;
    }
}

I have a method which takes in a WindowsProperties as a parameter

public WindowsWindow(WindowProperties props = WindowsProperties.Default)
{
    Init(props);
}

This gives me an error "Default parameter 'props' must be a compile-time constant"

What can I do to fix this?

Upvotes: 1

Views: 125

Answers (1)

Sir Rufo
Sir Rufo

Reputation: 19106

Use two constructors like

public WindowsWindow()
    :this(WindowsProperties.Default) 
{}

public WindowsWindow(WindowProperties props)
{
    Init(props);
}

Upvotes: 2

Related Questions