views:

71

answers:

3

Hi, this has me puzzled - I am no expert on page life cycle but I do not see why this is happening. It may be a simple case of where I declare my list. Here is the code:

public partial class feedback : System.Web.UI.Page
{
    List<Module> allModules = new List<Module>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            allModules = getAllModules();

            // Populate dropdown list of modules
            populateModulesDropDown();
...

The logic is this: the List 'allModules' get populated with Objects of a class called 'Module' in the getAllMethods() method. I have debugged and stepped through it testing on each step. allModules has a count of 9 as it should but when i step to the next line to run the populateModulesDropDown() method - the count is zero.. What is going on??

Any help would be awesome - thanks

Frank

A: 

Because you are not calling populateModulesDropDown with either a parameter of allModules or not invoking the instance of allModules.populateModulesDropDown

Woot4Moo
populateModulesDropDown does not use a parameter of allModules. It simply references it as this.allModules.
Frank
Ok lets approach this logically. You populate allModules via a call to getAllModules. Then you make a call to a static function that knows nothing about allModules. This is why your list is 0
Woot4Moo
Apologies I was returning a different list due to changing some code earlier and missing one line.. thanks for your help
Frank
agree. just try populateModulesDropDown(allModules). even if it doesn't work, you'll have improved your code ;) (it's kind of inconsistent that getAllModules() doesn't set the instance's field, but populate.. works with that field. avoiding such things helps avoiding problems like the one you're facing *even* if it turns out not to be the reason in this specific case)
Nicolas78
+1  A: 

Well, the List is a field in your class, so other methods in the same class will have access to it. Perhaps some method is clearing it or assigning it ? Try to use the IDE to find all references of the field, and look for any assignments to the field.

driis
Thanks but the only method doing anything to it in this part of code is the getAllMethods() which does clear allMethods before it fetches them all and adds them to it.. Any ideas?
Frank
A: 

I was returning a different List object from getAllModules() due to an overlooked line when making changes earlier. Apologies and thanks

Frank