C# 在winForm函数之外调用函数

C# 在winForm函数之外调用函数,c#,winforms,C#,Winforms,以下是我在WinForm应用程序C中的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using MyProject.Model; namespace MyProject.WinForm { publ

以下是我在WinForm应用程序C中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyProject.Model;

namespace MyProject.WinForm
{
    public partial class EcranLoginAPI : Form
    {
     

        public EcranLoginAPI()
        {
            InitializeComponent();

        }

        private void buttonTestConnexion_Click(object sender, EventArgs e)
        {
           var test = TestConnexionApi(textBoxApiKey, textBoxApiSecret);
        }
    }
}
这是我的班级:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyProject.Model
{
    public class Test
    {

        public bool TestConnexionApi(String apiKey, String apiSecret)

        {
            bool result = false;


            return result;

        }

    }
}
我尝试了一切,但我不知道如何调用TestConnexionApi。程序无法识别该函数

TestConnexionApi位于另一个名为Model的文件夹中


我将感谢任何帮助!非常感谢。对不起,这是个愚蠢的问题

我不想陷入一个完整的教程答案中,所以我会尽量让事情尽可能简单:

var test = new Test().TestConnexionApi(textBoxApiKey, textBoxApiSecret);
TestConnexionApi方法已在类中声明。要调用此方法,必须创建该类的实例:

Test myInstance = new MyProject.Model.Test(); //This will create an instance of your Test class
bool result = myInstance.TestConnexionApi(textBoxApiKey, textBoxApiSecret);
另一个选项是将测试类声明为静态:

namespace MyProject.Model
{
    public static class Test
    {
        public static bool TestConnexionApi(String apiKey, String apiSecret)
        {
            return false;
        }
    }
}
。。。然后调用TestConnexionApi方法,如下所示:

bool result = MyProject.Model.Test.TestConnexionApi(textBoxApiKey, textBoxApiSecret);
如果您是c语言入门,请从一些教程开始,并确保阅读有关静态类的内容。许多新程序员正在滥用static关键字。

怎么样

var test = new Test().TestConnexionApi(textBoxApiKey, textBoxApiSecret);

您正在调用该方法,就好像它在同一个类中一样,这肯定是不正确的。该功能是否标记为静态?你需要在你的问题中包含函数定义,以便有一个更好的答案。你好,梅森,我改进了我的帖子并添加了代码。该函数不是静态的。在另一个类中,我可以调用任何我不懂的函数。我是一个初学者,所以我可能会做一些愚蠢的事情…有很多事情需要解决。。。首先,您需要创建测试类的一个实例,以便调用它的一个方法:var Test=new Test。。。除非你创建一个静态类。哦,天哪,你是对的,我忘了实例化我的测试类。。。它现在起作用了。。。感觉好傻。。。对不起,非常感谢。你看到其他需要改进的地方了吗?它不是一个静态函数。它不必是静态的,你可以通过这种方式访问它。嗯。。。。你更新了你的代码。Lars的评论与您的原始代码一致。