[CS0120] Making an instance of a class is not an option here

Solved

i have a list List<string> pkey in public class login I can modify and read it fine in the login class but anywhere else I can't read it/modifiy how it would I go about doing it? something like Im using websocket-sharp

public class VerifyAccount : WebSocketBehavior
    {
        protected override void OnMessage(MessageEventArgs e)
        {
            
            if (login.pkeys.Contains(e.Data)){
                Send("Valid Key!");
            }else
            {
                Send("InvalidKey");
            }
        }
    }

this gives me CS0120, the solutions that people have been saying online is to make an instance of Login in this situation but that would not work for my use case, how would I go about reading it?

Posted Aug 12, 2022, 6:21 PM UTC

013
coolusername5599•

You need to have an instance of the class in order to access the instance properties of the Login class. You can pass in an instance of the class in the constructor of this class.

public class VerifyAccount : WebSocketBehavior
{

    private readonly Login login;

    public VerifyAccount(Login login)
    {
        this.login = login;
    }

    protected override void OnMessage(MessageEventArgs e)
    {
        
        if (login.pkeys.Contains(e.Data)){
            Send("Valid Key!");
        }else
        {
            Send("InvalidKey");
        }
    }
    
}
2
pixelkingliamOP•

i tried this but now i can't add VerifyAccount to my websocket server (wssv.AddWebSocketService<VerifyAccount>("/verify"))
i get CS0310

1
coolusername5599•

I'm not familiar with this library, but I think you could do it like this:

  1. Add a method to the behavior for setting the login
public class VerifyAccount : WebSocketBehavior
{

    private Login? login;

    public SetLogin(Login login)
    {
        this.login = login;
    }

    protected override void OnMessage(MessageEventArgs e)
    {
        if (login is not null && login.pkeys.Contains(e.Data)){
            Send("Valid Key!");
        } else
        {
            Send("InvalidKey");
        }
    }

}
  1. Use the initlizer action to set the behavior's login

    wssv.AddWebSocketService<VerifyAccount>("/verify", (b) => b.SetLogin(login));

1
Slypenslyde•

You said:

i have a list List<string> pkey

Your code is:

if (login.pkeys

The string pkeys is not the same as pkey.

But that's probably not it. You say you have:

public class login

And your code refers to:

login.pkeys

login is the class name. In order to refer to a member with just the class name, the list would have to be static. It's not. That's why C# is arguing you either need an object instance to refer to it OR you need to make it static.

Unfortunately that's not an easy fix. It means VerifyAccount needs access to some instance of login that you consider relevant, and there are lots of different ways to do that.

2
spca2001•

is login a static class?

1
pixelkingliamOP•

nope

-1
spca2001•

I think you either need to fill login class with data to work in its constructor and instantiate or make it static if you want to call it directly

1
pixelkingliamOP•

but if i make it static i cant declare variables within it

-1†
spca2001•

its hard to say with this snippet, but you cant call non static classes or members classes straight up

1
LondonPilot•

the solutions that people have been saying online is to make an instance of Login in this situation but that would not work for my use case

Why would that not work? The solution is to mark pkeys as “static” - but that’s generally considered bad practice, and if you let us know why creating an instance of Login won’t work, we can try to help you find a better solution.

0
pixelkingliamOP•

from my understanding of things i cant make a instance of Login called say, p1 , in main and use it somewhere, plus I'm not sure exactly how websocket-sharp would handle that,

I could maybe making login static but that would mean I couldn't declare/modify the list, right?

1
LondonPilot•

from my understanding of things i cant make a instance of Login called say, p1 , in main and use it somewhere,

Of course you can. You either need to pass it as a parameter to the place you want to use it, or use dependency injection (which comes built-in if you’re using ASP.Net, but can be set up with a bit of boilerplate code in other projects - but is difficult to add into a project at a late stage if the project wasn’t designed with it in mind).

plus I'm not sure exactly how websocket-sharp would handle that,

I’m not familiar with websocket-sharp, so I’ll leave that part to someone else, but I find it inconceivable that it wouldn’t allow you to use an instance variable.

I could maybe making login static but that would mean I couldn't declare/modify the list, right?

You can declare and modify the list. But there would only a single list throughout the entire application, shared between any part of code that accesses it. The alternative to this is… to create an instance.

2
pixelkingliamOP•

oh, making the list static fixed it for me, thanks!

1