Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I completely disagree with using the Obsolete property for this for a few reasons. </p> <p>To start with you will now have a warning for the ToString() method that you overrode and tagged with the Obsolete property:</p> <pre><code> [Obsolete("dont' use", true)] public override string ToString() { throw new Exception("don't use"); } </code></pre> <p>yields this warning: Warning 1 Obsolete member 'ClassLibrary1.Foo.ToString()' overrides non-obsolete member 'object.ToString()' d:\source\ClassLibrary1\ClassLibrary1\Class1.cs 11 32 ClassLibrary1</p> <p>so now you are stuck with a permanent warning in your code. On top of this, it doesn't exactly solve your issue. What happens when something in the framework implicitly calls ToString() now? The result of the following code is that the code in the body of ToString() is still called:</p> <pre><code> Foo myObj = new Foo(); Console.WriteLine(myObj); </code></pre> <p>So now you have a warning in your code, and it doesn't actually prevent a developer from doing the same thing all over again. I think the right move here is to try to find a way to throw an appropriate exception at run time rather than trying to mess with the .net object contracts.</p> <p>Suggestion for catching problem at compile time: I realized I had not previously given a suggestion for a solution for this issue. I don't really know what format your id is in for sure so I am only making a guess at it being an int, but why not protect whatever is creating the url with the querystring and pass the id as an int. That way a developer can't accidentally pass in some meaningless string without a compilation error. Like this for instance:</p> <pre><code>public string CreateItemUrl(int itemId) { return string.Format("someurl.aspx?id={0}", itemId); } </code></pre> <p>Now, calling this:</p> <pre><code>CreateItemUrl(myObj.Id); </code></pre> <p>becomes much more strongly typed and less error prone than:</p> <pre><code>string theUrl = string.Format("someurl.aspx?id={0}", myObj); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload