0

I have a ListView control which is definitely populated. The problem is that it's being treated as empty. When specifying an index of an item which I know is there, I get an "ArgumentOutOfRangeException" error.

public static string folderToUpdate(string folderChanged)
    {
        Main m = new Main(); // This is the class which this method is in,
                             // as a side matter I don't even know why I have to create
                             // an instance of the class I'm ACTUALLY IN
                             // (but I get a compile time error if I don't).

        string folder1 = m.lstFoldersToSync.Items[0].Text;
        string folder2 = m.lstFoldersToSync.Items[1].Text;

        string folderToUpdate;

        // If the folder changed is folder1 then the folder to update must be folder2.
        // If the folder changed is folder2 then the folder to update must be folder1.
        if (folderChanged == folder1)
        {
            folderToUpdate = folder2;
        }
        else
        {
            folderToUpdate = folder1;
        }

        return folderToUpdate;
    }

The error comes at line 8 where I declare and define "folder1".

Some Clarifications: I have searched extensively before posting. No other question is an exact duplicate. There are many similar questions but the solution always turned out to be either an empty list problem or an "Off-by-one error", neither of these are the problem I'm having. Once again I stress the Listview is populated (on form load).

So what can it be? Could the problem have something to do with Main m = new Main();?

Any help would be very much appreciated. Thank You.

PS: I think all the code besides lines 3, 8, and 9 are pretty much irrelevant (I may be wrong).

EDIT: I have solved the problem by using a static field list containing the contents of the "lstFoldersToSync" control, and then accessing that (instead of trying to access the contents of the control directly).

New working code:

    private static List<string> foldersToSync = new List<string>(); // This will be populated with the items in "lstFoldersToSync" control on "Main" form.

    public static string folderToUpdate(string folderChanged)
    {
       // Main m = new Main();

        string folder1 = foldersToSync[0];
        string folder2 = foldersToSync[1];

        string folderToUpdate;

        // If the folder changed is folder1 then the folder to update must be folder2.
        // If the folder changed is folder2 then the folder to update must be folder1.
        if (folderChanged == folder1)
        {
            folderToUpdate = folder2;
        }
        else
        {
            folderToUpdate = folder1;
        }

        return folderToUpdate;
    }

Thank you very much for all those helped me.

bfl
  • 403
  • 2
  • 8
  • are you intentionally using a static function? – attila May 09 '14 at 00:06
  • where is that code called from? some event? paste more code... – Z . May 09 '14 at 00:06
  • @attila Yes, it is static because I need to access it from another class (and I don't like instantiating, I know it's not the best). – bfl May 09 '14 at 00:09
  • @ZdravkoDanev It is a helper method used to determine which folder needs to be updated to match the other (but I think its irrelevant ). – bfl May 09 '14 at 00:11
  • @GrantWinney "an object reference is required for the nonstatic field method or property" – bfl May 09 '14 at 00:12
  • it should not be static for sure and you should not be creating an instance of the form for sure – Z . May 09 '14 at 00:13
  • to obtain the selected folder on the form the best practice is to create a property on the form. – Z . May 09 '14 at 00:14

3 Answers3

3

yes, your problem is with the m = new Main() part. You need somehow to obtain a reference to your form, not create a new one.

to obtain the selected folder on the form the best practice is to create a property on the form.

Z .
  • 12,657
  • 1
  • 31
  • 56
  • Can you please elaborate? Maybe provide some code? Thank you. – bfl May 09 '14 at 00:06
  • does the method live inside the main form? change it to be not `static` and remove the `m.` from lines 8 and 9. – Z . May 09 '14 at 00:08
  • That would certainly solve the problem except I need it to be static so that I can access it from another class (see my response to attila). – bfl May 09 '14 at 00:16
  • 1
    no, it should not be static, to access it from another class, you need to have a reference to the form. you can do something like the last response her: http://stackoverflow.com/questions/1000847/how-to-get-the-handle-of-the-topmost-form-in-a-winform-app – Z . May 09 '14 at 00:18
1

Because it's static, you'll need to pass a reference to your form/window or control to the method.

I'd consider rewriting the method so that you don't have to pass a reference to any UI elements.

public static string folderToUpdate(string folderChanged, List<string> foldersToSync)
{
    string folder1 = foldersToSync[0];
    string folder2 = foldersToSync[1];

    // Keep the rest the same
    ...
    ...
}

Then you could call it like this, sending in the list of folder names from your ListView:

yourClass.folderToUpdate("SomeFolderName",
                         lstFoldersToSync.Items.Select(i => i.Text);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Then "m" would be instantiated where? – bfl May 09 '14 at 00:26
  • But I can't write "lstFoldersToSync" from another class (which in case I didn't make clear I am doing). I get "The name 'lstFoldersToSync' does not exist in the current context" which it doesn't because "lstFoldersToSync" is in the "Main" class, and I'm calling "folderToUpdate" from _different_ class. – bfl May 09 '14 at 01:36
  • How can I do that ("somehow get a reference of your ListBox to that class")? – bfl May 09 '14 at 01:43
  • I have two classes one "Main" and one "other". In "Main" I have the "folderToUpdate" method, and in "other" I have a method which I want to call "folderToUpdate" – bfl May 09 '14 at 01:51
  • The problem is that the call to "folderToUpdate" is coming from a method in a different class which is why I made the "folderToUpdate" method static, causing the problem in the first place. – bfl May 09 '14 at 01:55
  • It's hard to explain but I can't do that (I have to access it from a different class so it _must_ be static (and I can't use instantiation because that would create a _new_ one and I need to access the _orginal_ one)). I have instead solved the problem by using a static field list containing the items in "lstFoldersToSync" and then accessing that. – bfl May 09 '14 at 17:23
0

I have solved the problem by using a static field list containing the contents of the "lstFoldersToSync" control, and then accessing that (instead of trying to access the contents of the control directly).

New working code:

private static List<string> foldersToSync = new List<string>(); // This will be populated with the items in "lstFoldersToSync" control on "Main" form.

public static string folderToUpdate(string folderChanged)
{
   // Main m = new Main();

    string folder1 = foldersToSync[0];
    string folder2 = foldersToSync[1];

    string folderToUpdate;

    // If the folder changed is folder1 then the folder to update must be folder2.
    // If the folder changed is folder2 then the folder to update must be folder1.
    if (folderChanged == folder1)
    {
        folderToUpdate = folder2;
    }
    else
    {
        folderToUpdate = folder1;
    }

    return folderToUpdate;
}

Thank you very much for all those helped me.

bfl
  • 403
  • 2
  • 8