【问题标题】:Error regarding switch expression (must be a bool, char, string etc.)关于 switch 表达式的错误(必须是 bool、char、string 等)
【发布时间】:2013-04-25 17:06:56
【问题描述】:

我正在向我的程序添加语音控制功能。我的代码有错误,我似乎无法摆脱。语音控制将允许用户在不使用鼠标或键盘的情况下控制程序的各个方面。

目前,我的代码包括:

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;
using System.Drawing.Printing;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using FontCombo;
using System.Speech.Recognition;
using System.Speech.Recognition.SrgsGrammar;
using Microsoft.VisualBasic;

if (voctrl.Checked == true)
        {
            vcstat.Text = "Voice Control Enabled";
            recognizer = new SpeechRecognizer();
            recognizer.SpeechDetected += recognizer_SpeechDetected;
            recognizer.SpeechRecognitionRejected += recognizer_SpeechRecognitionRejected;
            recognizer.SpeechRecognized += recognizer_SpeechRecognized;
            GrammarBuilder grammar = new GrammarBuilder();
            grammar.Append(new Choices("Cut", "Copy", "Paste", "Select All", "Print", "Deselect All", "Delete", "Save", "Save As", "Open", "New", "Close"));
            recognizer.LoadGrammar(new Grammar(grammar));
        }
        else
        {
            vcstat.Text = "Voice Control Disabled";
        }

private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (voctrl.Checked == true)
        {
            switch (e.Result.Text.ToUpper) //This is the error according to Visual Studio
            {
                case
                "Cut":
                    Clipboard.SetText(richTextBoxPrintCtrl1.SelectedRtf, TextDataFormat.Rtf);
                    richTextBoxPrintCtrl1.SelectedRtf = "";
                    break; // etc.

我已尝试在互联网上搜索解决方案,但似乎找不到与此代码一起使用的解决方案。我究竟做错了什么?如果我很愚蠢,我很抱歉,但我是 C# 和一般编程的新手。

错误如下:错误4 A switch expression or case label must be a bool, char, string, integer, enum, or对应的可空类型

【问题讨论】:

  • 括号...e.Result.Text.ToUpper()

标签: c# visual-studio .net-4.0 switch-statement


【解决方案1】:

ToUpper 应该是一个方法ToUpper(),你缺少括号

【讨论】:

  • 谢谢,但是在添加 () 之后,我得到了一些关于命令的奇怪错误。我很快再看一遍。
  • 哦。我有一些不正确的功能等。我现在已经修好了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 2020-10-01
  • 1970-01-01
  • 2014-12-13
  • 2011-06-12
  • 1970-01-01
相关资源
最近更新 更多