❔ Using `with` to create a new record in return statement result in an error
I'm trying to make new records using
with
statement, but it resulted in an error saying The name 'Y' does not exist in the current context
Can anybody help explain? Thanks28 Replies
Try
with { Y = Y + 1 }
and so on?Did it, still get the same error
ah, you can't do
Y++
like that
you need to do return position with { Y = position.Y + 1 };
also, this could (and should) be a switch expression
Point?
is the correct returntype, since it might return null.Thanks @Pobiega . I changed the code as you suggested and it indeed worked!! And the
switch
statement looks so much nicer than my ifs 😂
Would you mind explaining why I couldn't use Y++
there?its just not possible to use increment decrement operators with
with
sadly
its worth remembering that Y++
does two things - it both evaluates into a number, but also mutates the number
thats probably whyI see. Thank you.
Just one more unrelated question about this
Point? is the correct returntype, since it might return null.
nullable reference types ❤️
Does it mean I should always put
?
if any of my methods might return null
?yep. in fact, what .net version are you using?
6.0.402
its a language feature added in .NET 5, and since 6.0 it defaults to enabled
Ah, so it's not necessary for .NET >= 6?
erm, rather, Im suprised your IDE isnt yelling at you
I don't know 😂 I'm coming from Python and C# is still fairly new to me!
Anyway, thanks for your help. Really appreciate it! 😄
can read this if you want to know more https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types
Nullable reference types - C# reference
Learn about C# nullable reference types and how to use them
the idea is that any reference type can ofc be null
but that ability often causes issues during runtime
its annoying having to nullcheck things in every single method
so the idea was, "what if we add a way to document if a method can or can't return null"
consider these two methods
the bottom one can never return null
but the top one can, so we add a
?
to the return type to document that
this will give us warnings if we try to use that value without checking it for null
to prevent NullRefExceptions at runtime 🙂Let me read the documentation a bit more
Thank you for the useful info
yw
Now I noticed it, the IDE did yell at me via these lines
I tried to put
?
to where appropriate and the warnings left one by one except
here return new FountainOfObjects(name);
and here SendMessage(message);
my SendMessage
returns void
yeah but
message
might be null, and SendMessage probably doesnt take message?
so it wants you to make sure message is not null before passing it into SendMessageI think I did?
string? message = _world.GetMessage(_player.Position);
well, not really
you've stored it in a variable that says "this might be null"
you have not actually CHECKED if it is null or not
Ah I see
I checked for
null
within the SendMessage method instead of checking before sending it to the methodthats fine, but then your method could either accept
string?
or you move the check out 🙂Yeah, I moved the check out and managed to get rid off the warning of
null
at the method to instantiate a new object too. You're right, it could get a bit of annoying 😂
No more yellowy warning!!
Thanks very much for your helpnp
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.