Solved

how to call the variable out of the scope

Posted on 2012-03-12
4
311 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now