简体   繁体   English

从 switch 语句中访问多个变量 C#

[英]Access multiple Variables from within a switch statement C#

Good day, I'm after a bit of help.美好的一天,我需要一些帮助。

I'm currently trying to create a program that will access an EmailHandler.我目前正在尝试创建一个将访问EmailHandler 的程序。 that I'm writing.我正在写。

within said program, there are multiple "Addon" applications that have access to the same handler but have a different subject and body to be sent.在所述程序中,有多个“插件”应用程序可以访问相同的处理程序,但具有不同的主题和要发送的正文。

I've currently got it in a Switch statement like this.我目前在这样的 Switch 语句中得到了它。

           switch (mainMenu.SelectedApplication)
            {
                case "Application1":
                    {
                        LogHandler.Log(LogTarget.File, "Selected Application: Application 1 Queued");
                        string SUBJECT = "blah blah";

                        string BODY = "blah blah";
                    }
                    break;
                case "Application2":
                    {
                        LogHandler.Log(LogTarget.File, "Selected Application: Application 2 Queued");
                        string SUBJECT = "blah blah";

                        string BODY = "blah blah";
                    }
                    break;
                case "Application3":
                    {
                        LogHandler.Log(LogTarget.File, "Selected Application: Application 3 Queued");

                        string SUBJECT = "blah blah";

                        string BODY = "blah blah";
                    }
                    break;
            }

I've then got the Names of the applications coming through as SelectedApplication = "Application1";然后我得到了应用程序的名称作为SelectedApplication = "Application1"; etc..等等..

all that works fine.一切正常。 when I get the LogHandler to spout out the information within the selected Case当我让 LogHandler 输出所选案例中的信息时

however when I try to grab the data from the selected case但是,当我尝试从所选案例中获取数据时

I'm getting > The name 'SUBJECT' does not exist in the current context我得到 >当前上下文中不存在名称“SUBJECT”

when I try当我尝试

MailMessage message = new MailMessage();

message.Subject = SUBJECT;
message.Body = BODY;

etc

I'm still fairly new to C# so forgive me if it is an obvious answer.我对 C# 还是很陌生,所以如果这是一个obvious答案,请原谅我。

move SUBJECT and BODY out of switch将 SUBJECT 和 BODY 移出开关

    string SUBJECT = string.Empty;
    string BODY = string.Empty;

    switch (mainMenu.SelectedApplication)
    {
        case "Application1":

          LogHandler.Log(LogTarget.File, "Selected Application: Application 1 Queued");

                 SUBJECT = "blah blah";

                 BODY = "blah blah";
            
       ....
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM