Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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. 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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";
            
       ....
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading