Im currently working on modifying a server, which was written by another person. I now need to convert a non-static method into a static method. Everything works perfectly but this line of code inside the method:
var buOwner = SocketServer.GetClientByPlayerId(buUserId);
if (buChatProjectId == SocketServer.ChatProjectID.ToString() && buOwner != null && buOwner.Character.name == buUsername && buChatSessionId == buOwner.index.ToString() && ((byte)buOwner.Character.Faction).ToString() == buFaction)
{
buOwner.Chat = this;
}
throws an error because of the 'this':
error CS0026: Keyword 'this' is not valid in a static property, static method, or static field initializer
When I remove this line of code everything works and I can convert the method into a static one. Only downside is that this line seems to be doing something really important, as when I remove it the server doesn't work as intented anymore (doesn't execute all of the methods anymore etc. etc.) The thing is, I really have no clue what the buOwner.Chat = this; line is doing.
I know that with so little information it's hard to solve my problem, but the only thing I need to know is what a standalone 'this' does (or could be doing), as I have only ever seen it in combination with something (e.g. this.name) or as an extension method like: public static int Foo(this MyClass arg)
If I can understand what the 'this' could be doing in this line, I should be able to figure out how to solve my problem by exchanging the 'this' with something other that does the same thing.
So if any of you used 'this' like this (heh nice xD) before and could tell me what it does in such a use case, I'd be eternally grateful.
Some further code for background info:
The method that I want to convert to static
public static void OnMessage(string message)
{
var parsedMessage = message.Split('%');
var messageProtocol = parsedMessage[0];
var roomId = int.Parse(parsedMessage[1]);
Logger.Add("Server received message protocol: >" + messageProtocol + "<", "CDebug");
switch (messageProtocol)
{
case "bu": // Login Request
Logger.Add("Server received Login Request: " + " Protocol: " + messageProtocol + " | RoomID: " + roomId, "CDebug");
var buParsedBody = parsedMessage[2].Split('@');
var buUsername = buParsedBody[0];
var buUserId = buParsedBody[1];
var buChatSessionId = buParsedBody[2];
var buChatProjectId = buParsedBody[3];
var buChatLanguage = buParsedBody[4];
var buClan = buParsedBody[5];
var buFaction = buParsedBody[8];
var buOwner = SocketServer.GetClientByPlayerId(buUserId);
if (buChatProjectId == SocketServer.ChatProjectID.ToString() && buOwner != null && buOwner.Character.name == buUsername && buChatSessionId == buOwner.index.ToString() && ((byte)buOwner.Character.Faction).ToString() == buFaction)
{
buOwner.Chat = this;
}
The method that buOwner references (returns a client via the UserId)
public static Client GetClientByPlayerId(string id)
{
var clients = Clients;
foreach (var currClient in clients)
{
var client = currClient.Value;
if (client.playerId == uint.Parse(id))
return client;
}
return null;
}
My main thoughts were:
- Maybe the this just mirrors what is on the other side of the equal symbol | Nope, when I replace the 'this' with buOwner.Chat the server is still not running correctly
- Maybe it references what is written inside the if statement | Doesn't make much sense
- Maybe it stands for 'buOwner' | Nope when I replace 'this' with buOwner it throws a completely unrelated error
- Maybe it references the result of
SocketServer.GetClientByPlayerId(buUserId)| Nope as that's buOwner and I already tried that - Maybe it references the method | Nope that would be
public static void OnMessage(string message)and wouldn't make sense/help
This refers to the instance of the class it is used in. So if you were in an apple class, this would refer to that instance of the apple.
The reason something isn't working is because you're in a static method where this doesn't make sense since your not in an instantiated object.
To fix your issue, you will have to assign whatever the Chat object is without the keyword this. ie.
and GetChat() is getting the instantiation of your object you copied the code out of