Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String property is generated a nullable with no obvious way to mark it non-nullable #32

Open
R2D221 opened this issue Jan 9, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@R2D221
Copy link

R2D221 commented Jan 9, 2024

Describe the bug

I'm trying to declare a property for a custom YearMonthPicker. I'm adding a string property, and I'm expecting it not to be null because I'll be providing a default value. However, even tho the property itself is not marked as nullable, the generated code is:

    /// <summary>
    /// Default value: "MMMM yyyy"
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCode("DependencyPropertyGenerator", "1.4.0.0")]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public string? StringFormat
    {
        get => (string?)GetValue(StringFormatProperty);
        set => SetValue(StringFormatProperty, value);
    }

Steps to reproduce the bug

Declare a class, and add the following attribute:

[DependencyProperty<string>("StringFormat", DefaultValue = "MMMM yyyy")]

Expected behavior

    /// <summary>
    /// Default value: "MMMM yyyy"
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCode("DependencyPropertyGenerator", "1.4.0.0")]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public string StringFormat
    {
        get => (string)GetValue(StringFormatProperty);
        set => SetValue(StringFormatProperty, value);
    }

Screenshots

No response

NuGet package version

1.4.0

IDE

Visual Studio 2022

Additional context

No response

@R2D221 R2D221 added the bug Something isn't working label Jan 9, 2024
@HavenDV
Copy link
Owner

HavenDV commented Jan 11, 2024

Yes, in general this sounds logical.
But there are two ways here - make a Nullable parameter for the attribute with the default value true and the ability to make it false
Or make the property non-nullable automatically if DefaultValue is set. There may be problems here if under certain conditions this property may be null sometimes.

@R2D221
Copy link
Author

R2D221 commented Jan 15, 2024

I was thinking that, to guarantee a non-null value, a coerce-value callback should also be provided. Maybe having a NonNullable property could make the DefaultValue and Coerce method required, with the correct nullability for the method...

@HavenDV
Copy link
Owner

HavenDV commented Jan 16, 2024

Added in latest commit: 407759e

@BryceBarbara
Copy link

I'm unable to get this to work as I always get the following errors:

  • Partial method 'X.CoerceY(SomeType?)' must have an implementation part because it has accessibility modifiers.
  • Type 'X' already defines a member called 'CoerceY' with the same parameter types

Can you provide an example of how it should be used?

@HavenDV
Copy link
Owner

HavenDV commented Apr 15, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants