Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List with items returns empty

I have created a simple List function but if I Loop through the List it's empty. It should not be!

// List function 
    public class process_hook
    {
        public static List<String> pro_hook = new List<String>
                                              (new String[] { list_all_pocesses() });
        protected static string list_all_pocesses()
        {
            StringBuilder _list = new StringBuilder();
            foreach (Process i in Process.GetProcesses("."))
            {
                try
                {
                    foreach (ProcessModule pm in i.Modules)
                    {
                        pro_hook.Add(pm.FileName.ToString());
                    }
                }
                catch { }
            }
            return _list.ToString();
        }
    }


        // call 
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (String _list in process_hook.pro_hook)
            {
                Console.WriteLine(_list);
            }
        }
like image 994
User6996 Avatar asked Jan 14 '11 13:01

User6996


People also ask

What does an empty list Return?

The emptyList() method of Java Collections returns the list with no elements. This method is immutable.

Which method returns list or an empty list if nothing matches?

The findAll method returns an immutable IndexedSeq of all matching elements. If no elements match the query, findAll returns an empty IndexedSeq. It already returns an empty list if no elements are found, and will not cause an exception.

Is a list with null empty?

An empty collection isn't the same as null . An empty collection is actually a collection, but there aren't any elements in it yet. null means no collection exists at all.

What does an empty ArrayList return?

ArrayList isEmpty() in Java with example It returns true if the list contains no elements otherwise it returns false if the list contains any element.


1 Answers

Well this is a problem to start with:

catch { }

If anything goes wrong, you'll just silently abort.

Maybe that's what's happening? (EDIT: It is. See later.)

The next problem is that your "list" will only ever contain a single string... is that really what you intended? I doubt that the list you're seeing is actually empty - but it will contain a single empty string.

(As a side note, I would strongly suggest that you start following .NET naming conventions and avoid global variables like this.)

EDIT: Aargh - I've just realized what you've done. You're probably actually getting a NullReferenceException in list_all_pocesses, which you've caught and ignored.

Your call to pro_hook.Add is made before you've assigned a value to pro_hook. Basically you've got a variable initializer which uses a method which in turn uses the variable. Don't do that. If you step through your code in the debugger, you may get more of an idea of what's going on, but basically you've created a big ball of spaghetti for yourself.

Why doesn't list_all_pocesses just return a List<string>? Why are you using a StringBuilder at all?

like image 186
Jon Skeet Avatar answered Sep 25 '22 14:09

Jon Skeet