不能使用实例引用进行访问;用类型名称来代替它 [英] cannot be accessed with an instance reference; qualify it with a type name instead

查看:1013
本文介绍了不能使用实例引用进行访问;用类型名称来代替它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用示例1:在此我有以下代码,但出现以下错误:

I have the following code with the following error:

不能通过实例引用进行访问;用一种类型限定它 取而代之.

cannot be accessed with an instance reference; qualify it with a type name instead.

Program.cs

public static ThreadTest threadTest = new ThreadTest();
private static Thread testingThread = new Thread(new ThreadStart(threadTest.testThread()));
static void Main(string[] args)
{

}

ThreadTest.cs

public static void testThread()
{
}

推荐答案

您的testThread是静态方法,因此可以通过类型名使用.因此,请使用ThreadTest类型,而不是使用等距threadTest.

Your testThread is a static method, so it's available via type name. So, instead of using isntance threadTest, use ThreadTest type.

// public static void testThread()
testingThread = new Thread(new ThreadStart(ThreadTest.testThread));

或更改方法声明(删除static):

Or change method declaration (remove static):

// public void testThread()
testingThread = new Thread(new ThreadStart(threadTest.testThread));

此外,您应该传递方法以委派ThreadTest.testThread(除去括号),而不是传递方法调用ThreadTest.testThread()的结果.

Also you should pass method to delegate ThreadTest.testThread (parentheses removed) instead of passing result of method invokation ThreadTest.testThread().

这篇关于不能使用实例引用进行访问;用类型名称来代替它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆