[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C# Variable declaration not available to catch()

Posted on 2012-03-14
15
Medium Priority
?
290 Views
Last Modified: 2012-03-15
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.
0
Comment
Question by:kkbenj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
15 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 37722368
what's in the ....?

Are you declaring String EmailSubject anywhere else?
0
 

Author Comment

by:kkbenj
ID: 37722389
In the try{} block, EmailSubject is set to "Nope", catch sets it to "Bad" then finally sends an email using the EmailSubject variable.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37722395
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:kkbenj
ID: 37722405
I just want to reset the value of that variable, not declare a new one.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37722409
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
 

Author Comment

by:kkbenj
ID: 37722421
catch (Exception e)
{
    EmailSubject = Config.AppName + " -- Failed";
}

Open in new window


It's that simple
0
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 37722423
you're missing some code as well . . . where is your catch after the second try?
0
 

Author Comment

by:kkbenj
ID: 37722432
The second catch is there but all that functionality is there and working.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 37722754
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
 

Author Comment

by:kkbenj
ID: 37723192
So please give me a coded example of how I change the value of the variable.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 37723201
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37723205
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
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 37723209
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 37723605
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
 

Author Closing Comment

by:kkbenj
ID: 37724507
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question