C# Bool在当前上下文中不存在,但以与其他变量相同的方式声明

C# Bool在当前上下文中不存在,但以与其他变量相同的方式声明,c#,C#,我有一个bool,编译器说它不存在于当前作用域中,但是所有其他变量都是以相同的位置/方式声明和使用的。在下面的代码中,一些类名更改了,代码简化了,但结构保持不变 iDReq在当前上下文中不存在 if (button.Click) { string sourceFileName = ""; string destPathFileName = ""; int exportCount = 0; bool iDReq = true; iDReq = (System

我有一个bool,编译器说它不存在于当前作用域中,但是所有其他变量都是以相同的位置/方式声明和使用的。在下面的代码中,一些类名更改了,代码简化了,但结构保持不变

iDReq在当前上下文中不存在

if (button.Click) {
    string sourceFileName = "";
    string destPathFileName = "";
    int exportCount = 0;
    bool iDReq = true;
    iDReq = (System.Windows.Forms.MessageBox.Show("Include ID in file names?", "ID",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes); 
    foreach (KeyValueCollection item in SearchControl.SelectedItems)
    {
        Class.Document doc = Class.Document.GetDocument((long)item["id"].Value);
        {
            sourceFileName = @"\\server\share" + @"\" + Convert.ToString(doc.GetExtraInfo("docFileName"));
            string fileExtension = System.IO.Path.GetExtension(sourceFileName);     
            //Line below is the one that the compiler does not like.                                
            iDReq = true ? destPathFileName = destPath + @"" + doc.Description + " " + "(" + doc.ID + ")" + fileExtension : destPathFileName = destPath + @"" + doc.Description + fileExtension;
                try {
                    System.IO.Directory.CreateDirectory(destPath);
                    System.IO.File.Copy(sourceFileName,destPathFileName,true); 
                    System.Windows.Forms.Clipboard.SetText(destPathFileName);
                    exportCount ++;
                }
                catch(Exception ex)
                {
                    ErrorBox.Show(ex); 
                }
        }
    }
}

是因为它是一个布尔值还是我遗漏了什么?

我认为你的三元数写得不好,但我不确定你想要什么。我会将此重写为一个普通的
if/else
。我喜欢三元运算符,但它只是糖

我想你正在寻找这个:

destPathFileName = iDReq == true
    ? (destPath + @"" + doc.Description + " " + "(" + doc.ID + ")" + fileExtension)
    : (destPath + @"" + doc.Description + fileExtension);

iDReq==true
是超级函数

你也可以写:

destPathFileName = destPath + @"" + doc.Description
    + (iDReq ? " (" + doc.ID + ")" : string.Empty)
    + fileExtension;
顺便说一下,
@“
string.Empty

使用字符串插值:

destPathFileName = destPath + doc.Description
    + (iDReq ? $" ({doc.ID})" : string.Empty)
    + fileExtension;

我认为你的文章写得不好,但我不确定你想要什么。我会将此重写为一个普通的
if/else
。我喜欢三元运算符,但它只是糖

我想你正在寻找这个:

destPathFileName = iDReq == true
    ? (destPath + @"" + doc.Description + " " + "(" + doc.ID + ")" + fileExtension)
    : (destPath + @"" + doc.Description + fileExtension);

iDReq==true
是超级函数

你也可以写:

destPathFileName = destPath + @"" + doc.Description
    + (iDReq ? " (" + doc.ID + ")" : string.Empty)
    + fileExtension;
顺便说一下,
@“
string.Empty

使用字符串插值:

destPathFileName = destPath + doc.Description
    + (iDReq ? $" ({doc.ID})" : string.Empty)
    + fileExtension;
试试这个:

destPathFileName  = iDReq ? destPath + @"" + doc.Description + " " + "(" + doc.ID + ")" + fileExtension : destPath + @"" + doc.Description + fileExtension;
试试这个:

destPathFileName  = iDReq ? destPath + @"" + doc.Description + " " + "(" + doc.ID + ")" + fileExtension : destPath + @"" + doc.Description + fileExtension;

我一点也不清楚为什么要在条件运算符中执行赋值,但这行不通。不清楚您认为您分配给
idReq
的是什么。删除您的三元代码并编写一个if/else进行调试。我很确定三元if不会那样工作……三元运算符不会那样工作。
idReq
被分配给用户在消息框上的选择。我一点也不清楚您为什么尝试执行此操作条件运算符内部的赋值,但这不起作用。不清楚您认为您在那里分配给
idReq
的是什么。请删除您的三元代码,并编写一个if/else进行调试。我很确定三元if不会那样工作……三元运算符不会那样工作。
idReq
将被分配给消息框上用户的选择。@ALOISG无需将
idReq==true
。这只是另一种类型的答案
iDReq==true
不是问题。它只是一个超级字。@ALOISG不需要把
iDReq==true
。这只是另一种类型的答案
iDReq==true
不是问题。这真是太棒了。谢谢,这很有效,中间的那个。我想我的三元组是错误的语法。谢谢,中间那个有效。我认为我的三元语法是错误的。