2

The problem (C# compiler warning message):

warning CS0467: Ambiguity between method 'Microsoft.Office.Interop.Word._Document.close(ref object, ref object, ref object)' and non-method 'Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close'. Using method group.

The (partial..) solution: Compile time warning when using 'Microsoft.Office.Interop.Word._Document.Close'

The dilemma: If I explicitly cast to Microsoft.Office.Interop.Word._Document, ReSharper warns me that the "Type cast is redundant"

The question: Is there a real solution for this warning message, not just patchwork?

Community
  • 1
  • 1

2 Answers2

8

Just don't cast, C# doesn't require it:

Word._Application app = new Word.Application();
Word._Document doc = app.Documents.Add(...);
doc.Close(...);    // No ambiguity
app.Quit(...);
Pooven
  • 1,744
  • 1
  • 25
  • 44
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Got the warning CS0467 when I was using Word.Application and Word.Document - both without the starting _. Just adding the _ as your solutions shows solved the warning. – Bernhard Hiller Jul 10 '14 at 11:00
3

Well, if you need casting to avoid error... Cast !

Resharper has (good) generic rules, but sometimes they can't be applied.

You can "hide" the resharper warning message in this specific case ("Suppress inspection xxx with comment"), or... live with (if you can / are allowed to).

EDIT : ok, got the warning with lib version 12.0.0.0

So you can try

_Document w = new Document();

and you shall avoid c# and resharper warning.

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • Well..I'm doing a lot of code refactoring and I can not afford to "hide" any message :) If I was allowed, I would suppress directly the CS0467 Warning message from the very beginning. –  May 07 '12 at 12:15
  • could you give a little bit more code (at least "usings" and problematic code), version of Microsoft.Office.Interop.Word lib and version of Visual Studio, I can't reproduce on VS 2010 with lib version 14.0.0.0 – Raphaël Althaus May 07 '12 at 14:21
  • Microsoft Office.Interop.Word version 12.0.0.0, Visual Studio 2010 SP1 Microsoft Office.Interop.Word w = ...; w.Close(...); //compiler warning –  May 08 '12 at 07:54
  • Hmmm, look at my edit, it could help, but the code you give doesn't help much (I don't think w is of type Office.Interop.Word). If it doesn't help, more code is needed... – Raphaël Althaus May 08 '12 at 08:20
  • I check once again the code and it's like this: Document w = ...; If I go to the declaration of "Document" type, it sends me to the Microsoft.Office.Interop.Word namespace which has a public interface Document : _Document, DocumentEvents2_Event. –  May 08 '12 at 08:26
  • 1
    change Document w to _Document w as in my edit. The Close ambiguity is in fact an interface inheritance problem. – Raphaël Althaus May 08 '12 at 08:28