开发者

'Method' is used like a 'type' error C#

So I have this bit of code, that for some reason won't compile, because it says I am using a method as a type.... I for some odd reason can't see what it is that is supposed to be wrong, the only article google gave me, was an article here that is less than useless.

So my question is, what is wrong with this code:

static void开发者_如何学运维 Main() {
    Application.Run(new stat_checker());
}
public stat_checker() {
}


Well, you're calling new on it, so it looks like you want to declare a type called stat_checker:

public class stat_checker : Form
{
}

Alternatively, if you wanted to call stat_checker you'd need to write:

Application.Run(stat_checker());

But you also need to change stat_checker to be a valid type:

public Form stat_checker() {
    // Return something here
}

Were you trying to include this code within a type called stat_checker? If so, it should already work:

using System;
using System.Windows.Forms;

class stat_checker : Form {

    static void Main() {
        Application.Run(new stat_checker());
    }
    public stat_checker() {
    }
}

If you could tell us which of these scenarios you're actually trying to achieve, that would help. I'd also suggest that you start following .NET naming conventions, e.g. using StatChecker instead of stat_checker.


You are missing a return type in your method definition:

public ??? stat_checker() {

}

Without a return type this is considered to be a constructor of the stat_checker type. So if you intended this to be a constructor it has to be put in a class called stat_checker.


The Application.Run(); function takes a form as its argument for example: Application.Run(new Form1());


Depending on your intent, you need to do one of two things.

Either declare stat_checker as a class that is a Form,

static void Main() {
    Application.Run(new stat_checker());
}

public class stat_checker : Form {
// TODO: Implement the class
}

Or declare stat_checker as a method that returns a Form and remove the new,

static void Main() {
    Application.Run(stat_checker());
}
public Form stat_checker() { 
// TODO: Code that creates and returns a Form.
}


In your code, stat_checker is declared as a method; the new keyword is used to create an instance of a type (class, etc) - the two don't mix.

If stat_check is a method, then lose the Application.Run(new ...) and just call it directly.

Is this a console application? More details of what you're trying to accomplish would help us advise you.


The problem is that stat_checked is a function, not a class constructor for an ApplicationContext class, like it needs to be.

What you should do is something more like this:

class StatChecker : ApplicationContext
{
    public StatChecker()
    {
        Console.WriteLine("Hello, world!");
    }
}

Then you can instantiate it like this:

Application.Run(new StatChecker());

Note that your StatChecker class has to extent or return ApplicationContext. The typical "Form" class, for instance, extends ApplicationContext which is why it works.


Could it be as simple as mismatched Namespaces They would need to be the same in both classes to use the unqualified new stat_checker(). This bit me once which is how I got here... ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜