Variable "x" declared but error never used

Ok, so I have this great piece of code from Microsoft, and I have a little hiccups that I want to get rid of.

The source code prints to the ChangeConflictException x console, but I deleted this line. Now, every time I use this bit of code, I get an error: "The variable" x "is declared, but never used."

What is the most efficient way to get rid of this error while maintaining the functionality of the code?

//See http://msdn.microsoft.com/en-us/library/bb386918.aspx try { DB.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException x) { foreach (ObjectChangeConflict occ in DB.ChangeConflicts) { occ.Resolve(RefreshMode.KeepChanges); } } // Submit succeeds on second try. DB.SubmitChanges(ConflictMode.FailOnFirstConflict); 
+6
c # linq
source share
3 answers

The compiler is right; it could be just as good:

 catch (ChangeConflictException) { foreach (ObjectChangeConflict occ in DB.ChangeConflicts) { occ.Resolve(RefreshMode.KeepChanges); } } 

which limits the exceptions that fall into this block, but does not declare a variable for it. This variable is useful if you want to check a value, register it, or wrap it in another exception. Just for completeness (not applicable here), usually a second throw should be throw; , not throw x; (to save the stack trace).

+22
source share

Although in this case, you can simply get rid of the variable "x" (as mentioned by Mark). Typically, for scenarios where I have these warnings and cannot change the code (for example, using some fields by reflection), I usually prefer not to do anything to call the conditional compilation method, which suppresses such annoying warnings.

The code is below.

  catch (ChangeConflictException x) { DoNothingWith(x);// This suppress the 'x' not used warning foreach (ObjectChangeConflict occ in DB.ChangeConflicts) { occ.Resolve(RefreshMode.KeepChanges); } } [Conditional("Debug")] public static void DoNothingWith(object obj){ } 

MSDN link for conditional attribute: "Conditional method calls are enabled or dropped depending on whether this character is defined at the dial peer. If the character is defined, the call is on, otherwise the call (including the evaluation of the call parameters) is omitted."

+3
source share
 #pragma warning disable 0168 catch (ChangeConflictException x) { // code here } #pragma warning enable 0168 
+1
source share

All Articles