Nomei Kcooc
Nomei Kcooc

Reputation: 15

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 bear with me.

Upvotes: 0

Views: 3300

Answers (1)

Sehnsucht
Sehnsucht

Reputation: 5049

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.

Upvotes: 4

Related Questions