1

I've been trying to use multiple flags for the SpeechLibs Talk() function. This is what I am trying to do:

V.Speak ("Text", SpeechVoiceSpeakFlags.SVSFlagsAsync + SpeechVoiceSpeakFlags.SVSFIsXML);

However, it gives me following error:

Error 1 Operator '+' cannot be applied to operands of type 'SpeechLib.SpeechVoiceSpeakFlags' and 'SpeechLib.SpeechVoiceSpeakFlags' c:\users\max\documents\visual studio 2013\projects\switch\switch\default_tts_screen.cs 62 51 Switch

The documentation though clearly states that this should be possible:

Example
The following code snippet demonstrates the Speak method with several   commonly used flag settings.
[...]
V.Speak "text with XML", SVSFIsXML + SVSFlagsAsync
[...]

Please note that I am using C#, this however shouldn't change anything. Right?.. Please help me with this problem as it's already been smashing my head into my table since hours now. I haven't found a solution to this online.

Servy
  • 202,030
  • 26
  • 332
  • 449
Max
  • 13
  • 1
  • 5
  • Flags should be concatenated with the | operator, its binary equivalent to the + but addition operators are not defined for enums. – Ron Beyer Apr 30 '15 at 13:51
  • Not sure what language your example is in, but the + operator may be equivalent to | when using flags, or its converting them to an integer first and then adding them. You could have done `(SpeechVoiceSpeakFlags)((int)SpeechVoiceSpeakFlags.SVSFlagsAsync + (int)SpeechVoiceSpeakFlags.SVSFIsXML)` and it would work the same. – Ron Beyer Apr 30 '15 at 14:05
  • @RonBeyer C# Flags are not required to only have one bit set, so `+` isn't always equivalent to `|` – RoadieRich Apr 30 '15 at 14:06
  • @RoadieRich no, but if they are defined with the [Flags] attribute, they have to be in order to concatenate them correctly. https://msdn.microsoft.com/en-us/library/system.flagsattribute%28v=vs.110%29.aspx See the guidelines, second bullet point. Other than combinations, which then yes, + would corrupt what you want to do. – Ron Beyer Apr 30 '15 at 14:08
  • @RonBeyer It's a guideline, not a requirement. And the third bullet point contradicts that. – RoadieRich Apr 30 '15 at 14:11

1 Answers1

4

You combine enum flags with a bitwise OR operator as follows:

V.Speak ("Text", SpeechVoiceSpeakFlags.SVSFlagsAsync | SpeechVoiceSpeakFlags.SVSFIsXML);
spender
  • 117,338
  • 33
  • 229
  • 351
  • This is because you could have multiple flags combined in one enum value. For instance, you might have `enum Access {Read=1, Write=2, All=3)` If you set `myAccess = Access.Read`, then later on, you decide you need more access, `myAccess = myAccess + Access.All`, `myAccess` has the unexpected value of `4`. Using `|`, you get exactly what you want. – RoadieRich Apr 30 '15 at 14:04