Using vars assigned in a try statement

Here is my dilemma...

try
{
    MyObj myObj = new myObj();
    string passedVar = myObj.passThisVar;
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

Response.Write(passedVar);


I also tried
string passedVar;
try
{
    MyObj myObj = new myObj();
    passedVar = myObj.passThisVar;
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

Response.Write(passedVar);
JoshDaleAsked:
Who is Participating?
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.

DaHaCommented:
I am guessing that your issue is complaining received from the compiler in both cases?

You should often view a try/catch block as not guaranteed to run, and without that guarantee, your first attempt fails because passedVar may not have even been created, let alone initialized before your Response.Write(passedVar). A similar issue exists for the 2nd attempt, while passedVar may have been created, it has not been given an initial value, so it fails.

If all you want to do is display passedVar after the try/catch block and have it be given the desired value within (with nothing else needing to come out), simply give passedVar an initial value beforehand ala:
 
string passedVar = “”;
try
{
    MyObj myObj = new myObj();
    passedVar = myObj.passThisVar;
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

Response.Write(passedVar);

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
natejacobsCommented:
What is it writing out now, if anything, or what is happening?  
Try this to get a better idea of what's coming out:

string passedVar = "Not Set";
MyObj myObj = new MyObj();

Response.Write("Initial Value:  " + myObj.passThisVar;

try
{
    passedVar = myObj.passThisVar;
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

Response.Write("<br>Value of passedVar:  " + passedVar);
Response.Write("<br>Final Value:  " + myObj.passThisVar;
JoshDaleAuthor Commented:
Ok, I wasn't paying attention...
I thought the compiler was giving me the error, instead it was just a visual studio warning...

That worked fine, thanks.

string serverFile = "";
            try
            {
                // Upload the file
                UploadDocument uploadedDoc = new UploadDocument(txtFile);
                serverFile = uploadedDoc.serverFile;
            }
            catch (Exception ex)
            {
                FileUploadError.IsValid = false; // Trigger the custom validator
                FileUploadError.ErrorMessage = "An Error Has Occured<br/>" + ex.Message;

                // TODO: Insert the error into the database
            }

            try
            {
                // Parse the file
                string uploadedFile = ConfigurationManager.AppSettings["DocumentDirectory"] + serverFile;
                Index document = new Index(uploadedFile);
                Response.Write(document.PerformCorrectOperation());

                // TODO: Eliminate the Response.Write() and place the file contents into the database.
            }
            catch (Exception ex)
            {
                FileUploadError.IsValid = false; // Trigger the custom validator
                FileUploadError.ErrorMessage = "An Error Has Occured<br/>" + ex.Message;

                // TODO: Insert the error into the database
            }
JoshDaleAuthor Commented:
Also... what is with people posting their demo apps but not commenting anything??? ;)
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.