Solved

how to call the variable out of the scope

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

11 Experts available now in Live!

Get 1:1 Help Now