简体   繁体   English

MS Visual Studio C#-有人可以向我解释为什么我的代码不起作用以及如何使它起作用吗?

[英]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. 我正在编写一个程序,以便在Windows Active Directory中进行搜索,并将搜索结果返回到列表框中的另一个表单。

This is what my getter method looks like on the main form: 这是我的getter方法在主窗体上的样子:

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. 但是我的getter方法根本不能是静态的。

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. 在问这个问题之前,从我的研究看来,我需要拥有我的getter方法(因此是我的主要形式)的类的实例才能运行。 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 :C

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类型):

MainForm parent = (MainForm)this.Owner;
parent.getSearchResult();//CustomMethodName();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以向我解释为什么我的代码不起作用 - can someone explain to me why my code dosen't work 有人可以告诉我为什么我的标题没有显示asp.net C# - Can someone tell me why my header isn't showing up asp.net c# 有人可以向我解释这行C#代码吗? - Can someone explain this line of c# code to me? 有人可以正确地解释LINQ的交叉吗? 我不明白为什么这不起作用 - Can someone explain LINQ's intersect properly to me? I don't understand why this doesn't work 有人可以向我解释设置以及如何在C#中使用它们吗? - Can someone explain Settings to me and how to use them in C#? 不确定使用 lambdas 处理这个 c# 事件是如何工作的,有人可以向我解释一下吗? - Not sure how this c# event handling with lambdas work, can someone explain it to me please? 如何才能最好地显示此嵌套集合? (你能告诉我为什么我的代码无效吗?) - How can I best display this nested collection? (Can you tell me why my code isn't working?) 有人可以向我解释这个代码吗? - Can Someone Explain This Code To Me? 有人可以告诉我为什么这简单的C#代码不起作用,涉及从自身内部递归调用方法以获得根类别ID。 - Can someone tell me why this simple bit of c# code won't work, involves recursively calling method from within itself to get the root category ID 我为什么看不到我的 <example> s和<code>s from Intellisense? using C# and Visual Studio 2017-2019</code> <code>s from Intellisense? using C# and Visual Studio 2017-2019</code> - Why can't I see my <example>s and <code>s from Intellisense? using C# and Visual Studio 2017-2019
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM