Home > Enterprise >  Access multiple Variables from within a switch statement C#
Access multiple Variables from within a switch statement C#

Time:11-07

Good day, I'm after a bit of help.

I'm currently trying to create a program that will access an 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 (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"; etc..

all that works fine. when I get the LogHandler to spout out the information within the selected Case

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

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.

CodePudding user response:

move SUBJECT and BODY out of switch

    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";
            
       ....
    }
  • Related