0

I'm writing a program to conduct a search within Windows Active Directory and return the search results to another form in a listbox.

This is what my getter method looks like on the main form:

public List<String> getSearchResults()
{
    List<String> accountList = new List<String>();
    foreach (SearchResult account in searchResultCollection)
    {
        accountList.Add(account.Properties["cn"][0].ToString());
    }
    return accountList;
}

It is called only on the second form at load:

private void AccSelect_Form_Load(object sender, EventArgs e)
{
    List<String> accountList = Main_Form.getSearchResults();
}

However, the compiler tells me that "An object reference is required for a non-static method". But my getter method cannot be static at all.

From my research prior to asking this, it seemed like I would need an instance of the class that owns my getter method (so my main form) to be running. Which is fine since my first form is what instantiates the second form. The second form will never be running without the first form anyway.

Can anyone give me a possible solution to this? :C

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49

1 Answers1

1

when you need to call a method in the main form from the child form, can code like this (assumes your main form is of type MainForm):

MainForm parent = (MainForm)this.Owner;
parent.getSearchResult();//CustomMethodName();
too_cool
  • 1,164
  • 8
  • 24
  • Yay this worked! The only problem is that now it claims that the reference I made is null because there isn't an instance of my parent form (what??) and it throws a null pointer exception. – Katherine Lima Oct 20 '15 at 01:03