C# string array scope

The code below succeeds test 1, fails test 2.  How do I declare arrDataObject outside the if statement
so it is in scope for test 2?

            if(request.QueryString["dataobjects"] != null)
            {
                  string [] arrDataObject = request.QueryString["dataobjects"].Split(new Char [] {'~'}, StringSplitOptions.RemoveEmptyEntries);
                  response.Write("test 1" + arrDataObject[0] );
            }

response.Write("test 2" + arrDataObject[0] );
maxdbaseAsked:
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.

AndyAinscowFreelance programmer / ConsultantCommented:
string [] arrDataObject =null;
 if(request.QueryString["dataobjects"] != null)
            {
                  arrDataObject = request.QueryString["dataobjects"].Split(new Char [] {'~'}, StringSplitOptions.RemoveEmptyEntries);
                  response.Write("test 1" + arrDataObject[0] );
            }
if(arrDataObject != null)
  response.Write("test 2" + arrDataObject[0] );
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
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  You will need to test before the second response because arrDataObject could be null - which results in you program crashing if you try to access it when it is null.
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try like:

 How do I declare arrDataObject outside the if statement
so it is in scope for test 2?

  string [] arrDataObject;

            if(request.QueryString["dataobjects"] != null)
            {
                  arrDataObject = request.QueryString["dataobjects"].Split(new Char [] {'~'}, StringSplitOptions.RemoveEmptyEntries);
                  response.Write("test 1" + arrDataObject[0] );
            }
//try check the arrDataObject.length when necessary else you may get error below
response.Write("test 2" + arrDataObject[0] );
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
sorry, ignore my last comment as it may not work.
0
maxdbaseAuthor Commented:
Great solution!
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.