Null Warnings
I am making a class with 3 overloaded constructor, but I cannot bring myself to put the same code in 3 constructors, so I did it like this:But I get
CS8618 non-nullable property
warnings. How do I get rid of the warnings? Or is there a better way?15 Replies
Make your properties
required
I am not sure what you mean. I changed the optional part on all the constructor, and the warning is the same. It does not know that the method called handles all of the initialization. Can you show me what you mean?
public required string ErrMsg { get; private set; }
Thanks,
Any other solutions. I am on version 10.0 and that option is not available.
Ah, oof
In that case, I'd just throw away the
Initialize
method and just use constructorsis there a reason you aren't calling the other constructors with
this
?
yeahthere is also https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.setsrequiredmembersattribute?view=net-8.0
SetsRequiredMembersAttribute Class (System.Diagnostics.CodeAnalysis)
Specifies that this constructor sets all required members for the current type, and callers do not need to set any required members themselves.
: base
will be of use
uhI tried to call the other constructors first, but it said, cannot call it like a method
right, you have to call it like Z showed
: this
THANK YOU...., it is happy now
Instead of using an initializer function, I'd use the all-params constructor, calling it from the other ctor implementations:
er, I don't know how to scroll, evidently
oh, the conversation happened while I was typing it 😆
Since I have you all here... should I put that enum outside of the class? Not sure how I can call it outside of the class... should or can I add Static to it?
but I want the user (me) to indicate one of the 3
enum should be able to be referenced like
Text2Sql.InsertMode
Where it lives is kind of your preference imo
I'm also pretty sure enums are implicitly static
in the way c# uses the term