Link to home
Start Free TrialLog in
Avatar of johnwood
johnwood

asked on

About the underline

Hi,

What is the difference between
Word._Application word;
and
Word.Application word = new Word.Application();

When should I use which one?
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of johnwood
johnwood

ASKER

Thank you.

Yes, I am using
Word.Application word = new Word.Application();
Word.Document doc = word.Documents.Open(ref ....);

However, when doing doc.Close(ref miss, ref miss, ref miss); I got message:
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.

It seem some ambiguity between underline and without underline.....
Word.Application word = new Word.ApplicationClass();
not
Word.Application word = new Word.Application();
 
I think it's safe to disregard that warning - it's saying the document object has a method named "Close" and an event named "Close", and the compiler is choosing to use the method named "Close"
 
Or,
Word._Document doc = word.Documents.Open(...)
OK, disregard that warning.

Sorry, want to know more, why you said:
Word.Application word = new Word.ApplicationClass();
not
Word.Application word = new Word.Application();

I always use
Word.Application word = new Word.Application();
or similar things for Excel and Access and didn't find any problem??
Application is an interface, and when you instantiate it you actually get an object of type ApplicationClass (which implements Application). Probably six of one, half dozen of another, I just saw ApplicationClass used in a Microsoft example when I was first learning so that's what I stuck with.
 
Thank you.
Silly example, and not directly related to your question, but maybe interesting nonetheless.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
	public interface ITest
	{
		string Message { get; }
	}

	[CoClass(typeof(TestClass2)), ComImport, Guid("75578300-2CB7-4142-98F4-3CBC7F0BF504")]
	public interface ITest2
	{
		string Message { get; }
	}

	public class TestClass : ITest
	{
		public string Message { get { return "Hello World"; } }
	}

	public class TestClass2 : ITest2
	{
		public string Message { get { return "Hello World2"; } }
	}

	class Program
	{
		static void Main(string[] args)
		{
			//ITest a = new ITest(); // Fails, can't instantiate an interface
			ITest b = new TestClass(); // Works, TestClass implements the interface ITest
			ITest2 c = new ITest2(); // Works, because CoClass attribute of ITest2 says to actually return a TestClass2
			ITest2 d = new TestClass2(); // Effectively same as above line

			//Console.WriteLine("a is of type: {0}", a.GetType().Name);
			Console.WriteLine("b is of type: {0}", b.GetType().Name);
			Console.WriteLine("c is of type: {0}", c.GetType().Name);
			Console.WriteLine("d is of type: {0}", d.GetType().Name);

			Console.ReadKey();
		}
	}
}

Open in new window