Home » c# » CS0119 in compiling C#, Error at Task parameters-Exceptionshub

CS0119 in compiling C#, Error at Task parameters-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I’ve been working on a Discord Bot, and I get a really bad error.

I’m a beginner. I’m sorry if it’s a dumb error, I probably didn’t even knew C# basics more than a month ago.

private async Task Filter(SocketMessage arg)
{

}

public async Task StartInterpretation()
{
    await Filter(SocketMessage arg);
}

This is a small example that makes the error i’m getting. the await Filter(SocketMessage arg) causes this error:

error CS0119: ‘SocketMessage’ is a type, which is not valid in the given context

(the error is something with parameters as i can see.)

How to&Answers:

You’ll need to initialize arg, then pass into the Filter function. Something like:

public async Task StartInterpretation()
{
    var arg= new SocketMessage();
    await Filter(arg);
}

Answer:

The CS0119 compilation error is specifically because you are putting in a type parameter when you call a method. That’s not allowed, it’s only when declaring a method do you specify the type that it should expect.

However you have another issue where you are not instantiating arg. Below is your code updated so that it would compile.

// this is correct, SocketMessage is the type parameter that Filter expects
private async Task Filter(SocketMessage arg)
{

}

public async Task StartInterpretation()
{
    var arg = new SocketMessage();
    // do not specify the type parameter, just pass the variable
    await Filter(arg);
}