markjprice / cs11dotnet7

Repository for the Packt Publishing book titled "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals" by Mark J. Price
566 stars 206 forks source link

Example of CallerArgumentExpression - Page 182 #60

Closed FQanbari closed 1 year ago

FQanbari commented 1 year ago

Chapter: 4 Page Number: 182 Section Title: Logging information about your source code Step Number: Example of this section Problem to fix: The name 'condition' does not exist in the current context Suggested solution: using System.Diagnostics; // Trace using System.Runtime.CompilerServices; // [Caller...] attributes partial class Program { static void LogSourceDetails( bool condition, [CallerMemberName] string member = "", [CallerFilePath] string filepath = "", [CallerLineNumber] int line = 0, [CallerArgumentExpression("condition")] string expression = "") { Trace.WriteLine(string.Format( "[{0}]\n {1} on line {2}. Expression: {3}", filepath, member, line, expression)); } }

markjprice commented 1 year ago

You have not shown the code that caused your original exception but you probably entered the wrong code. The correct code is shown in the book:

[CallerArgumentExpression(nameof(condition))] string expression = "")

as shown in the following screenshot from the book: image

FQanbari commented 1 year ago

thank you for your response, I updated my visual studio and disappeared this error.