C# Variable declaration not available to catch()

Here is the error message from compiling:
A local variable named 'EmailSubject' cannot be declared in this scope because it would give a different meaning to 'EmailSubject', which is already used in a 'parent or current' scope to denote something else.

Where is the declaration incorrect?

class Program
{
  static void Main(string[] args)
  {
    string EmailSubject;
    try
    {
       EmailSubject="OK";
       ....

      try
      {
          CAN access EmailSubject fine in this block
      }
     catch (Exception e)
     {
          CANNOT access EmailSubject fine in this block
     }
     finally
     {
          CAN access EmailSubject fine in this block
     }
    }
  }
}

Open in new window


Thanks in advance.
kkbenjAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
what's in the ....?

Are you declaring String EmailSubject anywhere else?
0
kkbenjAuthor Commented:
In the try{} block, EmailSubject is set to "Nope", catch sets it to "Bad" then finally sends an email using the EmailSubject variable.
0
käµfm³d 👽Commented:
The catch can see (access) any variables which are outside of the try, but within Main (before the try). If you are declaring a new variable named EmailSubject within the catch, then you will get that error because you have two variables declared with the same name, within the same scope. Change the name of one or the other if you truly need both.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

kkbenjAuthor Commented:
I just want to reset the value of that variable, not declare a new one.
0
käµfm³d 👽Commented:
not declare a new one.
According to the error message, you are doing just that. Can you post the body of the affected catch?
0
kkbenjAuthor Commented:
catch (Exception e)
{
    EmailSubject = Config.AppName + " -- Failed";
}

Open in new window


It's that simple
0
Kyle AbrahamsSenior .Net DeveloperCommented:
you're missing some code as well . . . where is your catch after the second try?
0
kkbenjAuthor Commented:
The second catch is there but all that functionality is there and working.
0
AndyAinscowFreelance programmer / ConsultantCommented:
I agree with the other experts.
The error message you get is because you are declaring a variable with the same name twice (exactly where does not appear in your edited code that you show us).

eg.

...
string EmailSubject;
{  
  ...
  int EmailSubject;  //Here is the problem because you already have a variable of that name
0
kkbenjAuthor Commented:
So please give me a coded example of how I change the value of the variable.
0
anarki_jimbelSenior DeveloperCommented:
I took your code, just modified slightly to check compilation - see below
It works fine. Just try to find where you attempt to declare the same name variable.
I agree to other experts

    class Program
    {
        static void Main(string[] args)
        {
            string EmailSubject = "";
            try
            {
                EmailSubject = "OK";
                //....

                try
                {
                    //CAN access EmailSubject fine in this block
                    EmailSubject = "";
                }
                catch (Exception e)
                {
                    //CANNOT access EmailSubject fine in this block
                    EmailSubject = "";
                }
                finally
                {
                    //CAN access EmailSubject fine in this block
                    EmailSubject = "";
                }
            }
            catch (Exception ex)
            {

            }
        }//main
        
    }//class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
So please give me a coded example of how I change the value of the variable.
You already know how to do that, or at least you appear to in line 8 of your OP  ; )

As we said, you've got two lines in your code where you have the word "EmailSubject" preceded by some other word (a type). One is line 5 of your OP; the other you must find and remove the leading type. Then you should be good. We can review the exact code and get you sorted in short order, but only if we can actually see the exact code  = )
0
anarki_jimbelSenior DeveloperCommented:
Try to use Find functionality to search for "string EmailSubject" in your code. I'm pretty sure you will find more than one occasion. Remove second one
0
AndyAinscowFreelance programmer / ConsultantCommented:
You should not need to use Find - the compiler will tell  you at which line it is (part of the error message you have when you compile).
0
kkbenjAuthor Commented:
Very strange, this is the code I had that was throwing compilation errors.  After closing it out and re-opening, now it compiles w/o errors.
Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.