The correct way to send values ​​between classes

I asked a question before, but it sounds like I didn’t explain it in a good way, so here are the codes for the worst part (getting information from the package and storing them in x, y and there is a structure that accepts 2 ushort ref, so when I define an object, it changes the value of the variables that I send)

public class PacketHandler : GUI { GameUser role; public PacketHandler(GameUser who) { role = who; } ushort Actualx, Actualy; public PacketHandler(ref ushort x ,ref ushort y) { x = Actualx; y = Actualy; } public unsafe void HandleServer(byte[] data) { . . . case 10010: { if (BitConverter.ToUInt16(data, 8) == 1002) { Actualx = BitConverter.ToUInt16(data, 24); Actualy = BitConverter.ToUInt16(data, 26); } break; } 

this gets the value from the packages, saves them in actualx, actually getting ready to give them to any ushort ref request when defining an object parameter

there is another class here

 public class ClientBase { GameUser role2; public ClientBase(GameUser role) { role2 = role; Thread T = new Thread(HuntThread) { Name = "Hunt Thread" }; T.Start(this); } . . . public void HuntThread(object Sender) { ClientBase Client = Sender as ClientBase; while (true) { Monster Target = GetNextkill(); if (Target != null) { Thread.Sleep(1000); ProxyParadise.Network.Packets.PacketHandler getxandy = new ProxyParadise.Network.Packets.PacketHandler(ref X, ref Y); ProxyParadise.Network.Packets.PacketStructure ps = new ProxyParadise.Network.Packets.PacketStructure(); . . . 

and the real problem is that I find zero in x and y, when I track it, I find zero in actualx, actual, however I'm sure they got the value, so I think I'm doing something stupid

so overall, if u cannot help me in this code or figure out what I mean, then please tell me the correct way to get values ​​from another class when using streaming, thanks everyone, please, some kind of mod delete my old question, I rephrase it better now.

+4
source share
1 answer

Here we go:

  ushort Actualx, Actualy; public PacketHandler(ref ushort x ,ref ushort y) { x = Actualx; y = Actualy; } 

You assign values ​​from fields (which are initially zero) for parameters, and not values ​​from parameters to fields:

  ushort Actualx, Actualy; public PacketHandler(ushort x , ushort y) { Actualx = x; Actualy = y } 

Note. I also removed ref ; which would have an unpleasant side effect (in your source code) of wiping the by-ref variables that you passed in since the fields are zero at first.

However, what you cannot do is keep the ushort link as a field. If you need it, the best thing to do is dump the value onto the class and access the object:

 class Foo { public ushort Value {get;set;} } 

then any number of callers can have references to the same Foo object, and therefore updating one of them updates everything. Honestly, your PacketHandler already serve Foo instead; the trick would be for several code fragments to have a link to the handler and get them .ActualX / .ActualY from the object.

+4
source

Source: https://habr.com/ru/post/1433792/


All Articles