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

Chap 06: Comparing objects #93

Open
Strydir opened this issue Jul 14, 2022 · 4 comments
Open

Chap 06: Comparing objects #93

Strydir opened this issue Jul 14, 2022 · 4 comments

Comments

@Strydir
Copy link

Strydir commented Jul 14, 2022

Page 235:
the line "int result = x.Name.Length.CompareTo(y.Name.Length) results in the errors "'Name may be null here. Dereference of a possibly null reference".

To fix it, you must test to make sure that Name is not null first:

if((x is not null) && (y is not null))
{
if(x.Name is null) return 1;
if(y.Name is null) return -1;
int result = x.Name.Length.CompareTo(y.Name.Length);
}

Obviously, the errors disappear if you modify the file according to the "errata" for null values for page 235, but, as printed, the text does not work.
A possible VS Code version issue? Using VS Code version 1.69.1

@markjprice
Copy link
Owner

markjprice commented Jul 14, 2022

It's nothing to do with Visual Studio Code. It is a mistake in the printed book. The Person class has a Name property defined as a nullable string but that statement does not check for null. That's why the errata exists to fix that, as well as improve null handling in other ways too.

@Strydir
Copy link
Author

Strydir commented Jul 14, 2022

Well, it's not IN the errata, so I thought that I'd bring it to your attention. If you are planning on adding it, then great.
I find your response rather rude.
You're welcome for bringing it to your attention.

@markjprice
Copy link
Owner

Sorry! It was not my intention to be rude. The errata as it exists both fixes the issue and improves the code example, so there is nothing to add. That's all I meant.

@Strydir
Copy link
Author

Strydir commented Jul 15, 2022

Well, as a user, I went to your errata page specifically to find out why that page does not work and I find absolutely NO reference to the fact that there is an error in the book. I almost pitched the book (means to throw it in the garbage) because the book is apparently wrong but there is no reference to the error.
Please add a note stating that there is an error on the page so that dumb people like me don't spend so much freaking TIME trying to determine if I'M missing something!
Wow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants