Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to access that temp_list outside the scope. I want to use a list which stores those all elements which I can use it wherever I want. How do I do that?

More importantly, I want to expose that list to WPF so that I can print that list on one of the frames inside WPF.

What I have tried:

C#
string[] fileEntries = Directory.GetFiles(Gpath.selectedPath);
            Console.WriteLine(Gpath.selectedPath);
            //Console.WriteLine("HI");
            foreach (string fileName in fileEntries)
            {
                using (StreamReader reader = File.OpenText(fileName))
                {
                    string line = "";
                    string rline = "";
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line.Trim().StartsWith("<Path>"))
                        {
                            rline = line;
                            //Console.WriteLine(rline);
                            break;
                        }
                    }
                    //Console.WriteLine(rline);
                    rline = rline.ToLower();
                    string[] sep = { ",", "<", ">", "/", "", "scenario" };
                    string[] sline = rline.Split(sep, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string value in sline)
                    {
                        //int i = 0;
                        temp_list.Add(value);
                        //temp_list.Insert(i,value);
                        //i++;
                    }
                }
            }
            temp_list = temp_list.Distinct().ToList();
           // Console.WriteLine(temp_list);
            //temp_list=string.Join("",temp_list);
            // Console.Write(string.Join(",",temp_list));
           // display_topology(temp_list);
            Console.WriteLine("All available topologies");
            for (int i = 1; i < temp_list.Count; i++)
            {
                Console.WriteLine(i.ToString() + "." + temp_list[i]);
                //display_topology(temp_list);
                //temp_list[i];
            }
            //temp_list[i];
           }
Posted
Updated 26-Jan-22 4:50am
v2
Comments
Richard Deeming 26-Jan-22 7:27am    
There is nowhere near enough detail here for anyone to be able to answer your question.

At a guess, the temp_list is declared as a local variable within the method, which means it won't be visible to anything outside of the method.

But that is just a guess, since you haven't shown where temp_list is declared, nor adequately explained what you want to do with it.
DivyaHal 26-Jan-22 9:02am    
Basically my folder contains files of type XML.I am read files line by line from the particular line c. Going to particular line that starts with path tag and then splitting the line to get content within the tag. And then adding that content to temp_list. So that I can display that list onto the frame. I can display easily all the data present in temp_list on console. I just don't know how to pass that temp_list to WPF or something.

If you declared temp_list inside a method, then it is local to that method and nothing outside it can access it at all (and when the method ends, then stack space that was allocated to the variable will be "recycled" so even if you could access it, it wouldn't help.

But the List that temp_list references is on the heap (because List is a reference type) so provided you copy the reference into a variable outside the method, the data isn't lost.
There are two ways to do that:
1) Create a variable outside the method (i.e. at class level with public or private) and copy the reference into that before the method ends.
2) Return the List to the caller when the method exits and let it deal with it.

We have no idea how that code fits into the rest of your application, so we can;t say "do this" and it'll work. You need to think about your code architecture and decide how best to implement it.
 
Share this answer
 
Comments
DivyaHal 26-Jan-22 10:36am    
I created global list and added temp_list contents to it.
And. Then tried to access it. But it still doesn't work. I am very new to this whole thing I am trying to learn. I have applied my all learnings still do not know what I am missing.

final_list is declared globally
For(int i=0;i
OriginalGriff 26-Jan-22 11:07am    
"it doesn't work" is one of the most common problem reports we get here.
It's also the most useless, because it tells us nothing at all about what your problem actually is.

And we don't know, because we can't see your screen, we can't access your HDD, and we can't read your mind.
So explain it to us: tell us what you did to cause the problem. Tell us what happened when you did that. Show us any error messages, show us which line it occurred on if you can.

But "it doesn't work" tells us absolutely nothing.
Quote:
Trying to access list outside the scope

Short answer:
The principle: A variable do not exist outside of its scope.
The only solution: change the scope accordingly to your needs.
 
Share this answer
 
v2
Comments
DivyaHal 26-Jan-22 10:36am    
I did change the scope like making my variable global. But still doesn't work. I do not understand what I am missing.
Patrice T 26-Jan-22 11:08am    
Your code show usage of the variable, but no declaration.
We have no idea what you are doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900