3

For whatever reason, the initialized value of a value struct defined in Windows Runtime Component is ignored in the app (C#).

CX/C++:

namespace RuntimeComponent1
{
    public value struct Foo {
        bool flag1 = true;
        bool flag2 = false;
    };
}

C#

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        var settings = new RuntimeComponent1.Foo();
        Debug.WriteLine(settings.flag1); // Output: False
        Debug.WriteLine(settings.flag2); // Output: False
    }
}

Notice that flag1 should be True but instead it is False. Why? !


Edit: As suggested by @HansPassant, create a ticket for this: https://connect.microsoft.com/VisualStudio/feedback/details/2702659 If you also think this is a problem. Please help up voting it.

Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • 3
    Not all languages support constructors for value types (e.g. JS). Therefore, WinRT does not support constructors for value types. (More accurately, constructors for value types are not projected.) – Raymond Chen May 13 '16 at 14:34
  • @RaymondChen kind of make sense. I guess I am more upset about that fact that there is no warnings/errors on this than the fact that it is not supported. – Yuchen May 13 '16 at 14:43
  • Feel better about it by reporting this at connect.microsoft.com – Hans Passant May 13 '16 at 15:14
  • 1
    I agree that there should be a warning/error. – Raymond Chen May 13 '16 at 16:34

1 Answers1

1

I have got an email update from Microsoft VC++ team after posting the issue on Microsoft connect https://connect.microsoft.com/VisualStudio/feedback/details/2702659

Thank you for reporting this issue. The next release of the Visual C++ Compiler Toolset will issue an error when a member of a value class has a default member initializer.

Yuchen
  • 30,852
  • 26
  • 164
  • 234