Solved

how to call the variable out of the scope

Posted on 2012-03-12
4
312 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
  • 2
  • 2
4 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 500 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

825 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