Help with "Does not exist in current context" error

Near the bottom of this code you can see that "spUser" is underlined in red.  The error is that spUser does not exist in the current context.  I'm sure it's the way I've defined it in the "if" statement above.  

How can I create the spUser variable in the if statement so that it can be used in the lines below (in the using block)?

Code snippet(You can click on this image to make it larger to read)

Thank you
greddinAsked:
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.

p_davisCommented:
You would need to put  the declareation of the Variable outside of the if statement so it can be referenced by the whole scope of the method
greddinAuthor Commented:
Is there a way I can initialize the SPUser spUser to empty or null out before of the if statement then let the if / else set accordingly?
p_davisCommented:
If it is in the method u might have to new it up... Otherwise you could instantiate as null as a class membet
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

greddinAuthor Commented:
Would you kindly show me a line or two of code for what you mean?  Thanks.
käµfm³d 👽Commented:
Based on the logic you have written, you shouldn't have to new anything outside of the if. Your if/else assigns to that variable in both blocks, so there is no chance that you have an uninitialized variable by the time the line in red gets hit--provided you don't refactor any of those bits. You can simply move the declaration of the variable out of the if/else.
greddinAuthor Commented:
The problem is that the spUser is different depending on the if condition.  If there's an ID querystring then spUser is looked up based on the ID.  But if ID is empty then spUser is taken as the current user.  I don't think I could move it out of the if/else for this reason.
p_davisCommented:
It seemed to me that the initialization was conditional.... So it would need to be outside of the if
käµfm³d 👽Commented:
You're only moving the declaration:

SPUser spUser;
You're not moving the other parts of the logic. Just delete the SPUser bit--i.e. the type--from the if/else blocks.
p_davisCommented:
Void yourmethod()
      Var yourvariableavailabletothemethod = mill
If
  Instantiate
Else
  Instantiate

//variable is still in scope... Otherwise declare outside o f meth o d. Making it a class member

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
p_davisCommented:
Mill = null
p_davisCommented:
If declared in t h e method it would have to be given a value, such as null... If it is a class member you can initialize it later
käµfm³d 👽Commented:
If declared in t h e method it would have to be given a value, such as null...
It only has to be given a value prior to use (i.e. reading). He's got that covered based on how the if/else is written. Otherwise, I would agree that the variable would need to be initialized.
greddinAuthor Commented:
Thanks to both if you.
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.