3

I am using Microsoft Text-to-Text Speech feature in my project. But I have a question about that, actually not directly about that. So :

Normally programmers when creating Interface, they put I as a prefix of the interface name like IReadable,IEnumerator etc. But I've come across something that actually shocked me.

in Microsoft Text Speech DLL there is something like this : SpVoice which is interface (they didn't put I as prefix for some reason and I don't know why ?) and SpVoiceClass. So then what's the problem you may ask, Here :

SpVoice speak= new SpVoice(); //I created an object from SpVoice Interface
speak.Speak("Hello StackOverFlow"); //and it speaks and say exactly what I write.

and

SpVoiceClass speak =  new SpVoiceClass();
speak.Speak("Hello Kowanichi"); //and it does the same thing.

The thing I don't get is how ? How does the first one work although it says it is an interface with tons of unimplemented methods etc.

Please some one explain me HOW ?

I am really confused now and maybe Microsoft developers didn't put I prefix for that reason, it can be instantiated.

Thanks in advance. Hope I describe my problem clearly.

Here is the image that I want you to see : alt text
(source: pixelshack.us)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Tarik
  • 79,711
  • 83
  • 236
  • 349

3 Answers3

7

EDIT: Ah, I see. Look at the definition of SpVoice:

[CoClass(typeof(SpVoiceClass))]
public interface SpVoice : ISpeechVoice, _ISpeechVoiceEvents_Event
{}

Then look up the CoClass attribute:

A coclass supplies concrete implementation(s) of one or more interfaces. In COM, such concrete implementations can be written in any programming language that supports COM component development, e.g. Delphi, C++, Visual Basic, etc.

I'm not familiar with this, so don't take this as gospel, but it appears that, through compiler magic triggered by the CoClass attribute, you're instantiating an instance of SpVoiceClass when it looks like you're instantiating an interface.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • I just imported Microsoft Speech Object Library into my project from COM. – Tarik Jun 06 '09 at 04:00
  • and you can see the SpVoiceClass from the picture I put. – Tarik Jun 06 '09 at 04:04
  • and would you tell me why I'd need a coclass ? – Tarik Jun 06 '09 at 04:51
  • Do you mean, why does that attribute exist on that class when you could simply instantiate SpVoiceClass? I don't know, but I'd guess convenience - maybe an old, primitive way to implement a Factory pattern. – Michael Petrotta Jun 06 '09 at 04:54
  • Well according to what I've just read, it must be used when declaring COM Classes in your wrapper library. – Tarik Jun 06 '09 at 05:36
0

If you can do SpVoice speak= new SpVoice() and didn't get an error, then it means SpVoice has a constructor, which means that is is NOT an interface.

J.W.
  • 17,991
  • 7
  • 43
  • 76
  • It's an interface. You can check it out yourself if you don't believe me. – Tarik Jun 06 '09 at 03:55
  • I edited my initial post and put a picture of it. Right below my question you can see the Hint when I bring the mouse over the instance. – Tarik Jun 06 '09 at 04:01
0

Dont take the "I" thing too far. Its just a naming convention. Perhaps spVoice is not an interface. May I know how you concluded spVoice is indeed an interface ?

If it is an interface it cant be instantiated.

Sathya
  • 2,371
  • 5
  • 19
  • 27