2

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.

2
  • 1
    Understanding scope and visibility Your case falls in the block level paragraph Commented Nov 7, 2021 at 13:47
  • 1
    You need to declare SUBJECT and BODY in the correct scope. Commented Nov 7, 2021 at 13:47

1 Answer 1

2

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";
            
       ....
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I'm an idiot I knew it would be something stupid that I missed. Thanks Serge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.