✅ Null check not enough to safely convert from nullable enum

I have a class declaring an enum and a nullable value for that enum:
public readonly StdModeTypes? smt;
public readonly StdModeTypes? smt;
I then try to reference that field from a second class:
if(cmodeCur.smt != null)
mapModesWithStdTypes[cmodeCur.smt] = cmodeCur;
if(cmodeCur.smt != null)
mapModesWithStdTypes[cmodeCur.smt] = cmodeCur;
Yet even with the null check, the IDE is insisting that smt could be null and won't let me use the value:
Error CS1503 Argument 1: cannot convert from 'BestChat.IRC.Data.Defs.ChanMode.StdModeTypes?' to 'BestChat.IRC.Data.Defs.ChanMode.StdModeTypes'
Error CS1503 Argument 1: cannot convert from 'BestChat.IRC.Data.Defs.ChanMode.StdModeTypes?' to 'BestChat.IRC.Data.Defs.ChanMode.StdModeTypes'
What's going on?
20 Replies
Will Pittenger
Will Pittenger9mo ago
Given the smt field is readonly, isn't that enough to ensure the value hasn't changed?
mindhardt
mindhardt9mo ago
With value types the nullability is different With ref types it is just an annotation, more or less
Will Pittenger
Will Pittenger9mo ago
OK.
mindhardt
mindhardt9mo ago
But with value types it is an actual type, in your case Nullable<StdModeTypes>
Will Pittenger
Will Pittenger9mo ago
Too bad the compiler doesn't automatically unwrap it for you when the value can't be null.
mindhardt
mindhardt9mo ago
if(cmodeCur.smt.HasValue)
mapModesWithStdTypes[cmodeCur.smt.Value] = cmodeCur;
if(cmodeCur.smt.HasValue)
mapModesWithStdTypes[cmodeCur.smt.Value] = cmodeCur;
.HasValue is the same as != null or is not null In this case
if(cmodeCur.smt is { } smt)
mapModesWithStdType[smt] = cmodeCur;
if(cmodeCur.smt is { } smt)
mapModesWithStdType[smt] = cmodeCur;
This is also an option if you like patterns
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Yup
Yup9mo ago
I have the ultimate solution for this
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Yup
Yup9mo ago
PHP
MODiX
MODiX9mo ago
That command had an error
UnknownCommand: Unknown command.
Remove your reaction to delete this message
mindhardt
mindhardt9mo ago
$bam
MODiX
MODiX9mo ago
User bammed successfully.
Yup
Yup9mo ago
Unknown command $bam @Hin
MODiX
MODiX9mo ago
User bammed successfully.
Will Pittenger
Will Pittenger9mo ago
Bam?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
mindhardt
mindhardt9mo ago
Yup
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Will Pittenger
Will Pittenger9mo ago
Yes. Thanks. Used to classes.
Want results from more Discord servers?
Add your server
More Posts
❔ How do I change the color of certain targets when they're shot, and also destroy others when shot?I have tagged the ones to be destroyed as destroy and the ones that change color as color, but neith❔ unity single jumpHi, I need some help about jump mechanics in unity, I have written a code that suppose to make the p❔ i don't have enough (any) experience with semaphores to know if my wpf app requires themi started a new role very recently and my first project is helping a client, an electrical engineer,❔ Wpf massive memory overheadHey guys, I have this wpf window which enables me to select a rectangle on the screen. It gets open✅ Stored Procedure HelpHello, I have a stored procedure in my project called `spGetVendor`. It takes one parameter, `@Vendo❔ Console.ReadLine != nullHi all, I'm went trough this Microsoft challenge (unsuccessfully) and after reviewing this solution✅ New to wpf and need some help with architecture.I have a game-listener that gets new information every second. I crreate an instance and start the ✅ Kinda new to MVC/C#/Razor - Need some help passing data from Model -> ControllerAs the title states, I'm kinda new to C#/MVC with a good bit of experience with software development✅ WPF Custom control should have contents as DockPanel, but it shows up at runtime as ScrollViewerI have a `UserControl` I'm implementing with the source shown below. When I check my custom controlrider dont see the android sdkI download android sdk, but rider dont see it