• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

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?
0
johnwood
Asked:
johnwood
  • 5
  • 3
1 Solution
 
Todd GerbertIT ConsultantCommented:
_Application and Application are both interfaces, and at first glance it looks like _Application is just one piece of Application (that is to say, Application inherits from _Application along with some other interfaces).
The correct usage, I believe, is Word.Application word = new Word.ApplicationClass();
0
 
johnwoodAuthor Commented:
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.....
0
 
Todd GerbertIT ConsultantCommented:
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"
 
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Todd GerbertIT ConsultantCommented:
Or,
Word._Document doc = word.Documents.Open(...)
0
 
johnwoodAuthor Commented:
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??
0
 
Todd GerbertIT ConsultantCommented:
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.
 
0
 
johnwoodAuthor Commented:
Thank you.
0
 
Todd GerbertIT ConsultantCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now