Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to call the variable out of the scope

Posted on 2012-03-12
4
Medium Priority
?
319 Views
Last Modified: 2012-03-12
Hi All,

Am just trying call "paths" in my foreach loop but it keeps on saying its out of scope bcoz i declare the values inside the if loop.

Someone please enlighten me how to call this variable from foreach loop ?

This is the one am calling in my foreach loop "  string[] paths = { Firstpath, SecondPath, ThirdPath };" it depends on the if condition.

Thanks in Advance

            string[] pathsplit = path.ToString().Split('/');
           if(pathsplit[3].contains{"testFile"))
{
            string Firstpath = pathsplit[0] + "/" + pathsplit[1] + "/" + pathsplit[2] + "/" + pathsplit[3] + "/";
            string SecondPath = Firstpath + "/" + pathsplit[4] + "/";
            string ThirdPath = SecondPath + "/" + pathsplit[5] + "/";
            string[] paths = { Firstpath, SecondPath, ThirdPath };
}
else
{
        string Firstpath = pathsplit[0] + "/" + pathsplit[1] + "/" + pathsplit[2] + "/" + pathsplit[3] + "/";
            string SecondPath = Firstpath + "/" + pathsplit[4] + "/";
       string[] paths = { Firstpath, SecondPath };
}
            foreach (string pat in paths)
            {
               bool result= dir.EnsureDirectoryExists(pat);

                if (result==true)
                {
                    //do nothing
                }
                else
                {   //create dir
                    dir.createdir(pat);
                }
            }
            upload(path,filename);

        }
        

Open in new window

0
Comment
Question by:Sha1395
[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
  • 2
  • 2
4 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 2000 total points
ID: 37708680
Before the if statement declare:

string[] paths = null;

Open in new window


then inside the if statement assign it:

paths = { Firstpath, SecondPath, ThirdPath };

Open in new window


and

paths = { Firstpath, SecondPath };

Open in new window

0
 

Author Comment

by:Sha1395
ID: 37712166
thanks dale_burrell,i tried to declared

string[] paths = null; outside the if statement but got the different error  @paths = { Firstpath, SecondPath, ThirdPath }; it throws ";" expected.

Am not sure why it throwing this error
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 2000 total points
ID: 37712181
try

paths = new string[] { Firstpath, SecondPath, ThirdPath };

Open in new window


http://msdn.microsoft.com/en-us/library/aa287601(v=vs.71).aspx
0
 

Author Comment

by:Sha1395
ID: 37712189
i got it,it suppose to be declare like

Path= new string[] { Firstpath, SecondPath, ThirdPath }; in the if statement
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

598 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