Nomei Kcooc Nomei Kcooc - 1 month ago 8
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)
{
base.Init();
try
{
this.sock.ReceiveTimeout = 5000;
this.sock.Connect(ip, 5055);
this.PingBytes[this.PingBytes.Length - 1] = this.PingId;
this.sock.Send(this.PingBytes);
//this.PingBytes[this.PingBytes.Length - 1] = (byte)this.PingId - 1;
}
catch (Exception value)
{
this.sock = null;
Console.WriteLine(value);
}
return false;
}


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

Answer

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.

Comments