Nomei Kcooc Nomei Kcooc - 2 months ago 29
C# Question

Error: CS0266 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)

Know I cast a byte to the code that I have commented but I still have the same error

public override bool StartPing(string ip)
this.sock.ReceiveTimeout = 5000;
this.sock.Connect(ip, 5055);
this.PingBytes[this.PingBytes.Length - 1] = this.PingId;
//this.PingBytes[this.PingBytes.Length - 1] = (byte)this.PingId - 1;
catch (Exception value)
this.sock = null;
return false;

I'm not that good with setting the format yet so please bare with me.


The whole expression has to be casted :

(byte) (this.PingId - 1);

Otherwise PingId is casted to byte and then 1 is subtracted from it and as 1 is an int the byte is promoted back to an int.