Katherine Lima
Katherine Lima

Reputation: 1

MS Visual Studio C# - Can someone explain to me why my code isn't working and how I can get it to work?

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

Upvotes: 0

Views: 131

Answers (1)

too_cool
too_cool

Reputation: 1194

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();

Upvotes: 1

Related Questions