send int thru I2C

hi all.

i just a beginner hobbyist with Arduino. or i used to play with it the last 2 years. no programming experice.

but i tried to send a Unsigned Int thru I2C.

i have seen many differnt examples on the interweb, i also tried I2C_anthing. but i never understead it. ot got it working.

now i made my own Code that seems realy easy and i wonder why i never seen it before, or maby you can tell me if its good or whats wrong about it. but in the end it seems to work Perfect at my side.

so the quation is why did i not see this realy EASY!! code before.

Master

#include <Wire.h>

unsigned int Var = 1;
unsigned int lastVar = 0;
byte A;
byte B;

void setup(){
Wire.begin();
}

void loop() {
Var++;
if (Var != lastVar) {

    A = Var % 256; //Repeat 256 (16bit)
    B = Var / 256; // divide 65536 by 256 (8bit)

    Wire.beginTransmission(1);
    Wire.write("a");
    Wire.write(A);
    Wire.endTransmission();
    Wire.beginTransmission(1);
    Wire.write("b");
    Wire.write(B);
    Wire.endTransmission();
  }
lastVar = Var;
}

Slave

#include <Wire.h>
byte a;
byte b;
unsigned int Var;

void setup(){
  Serial.begin(9600);
  Wire.begin(1);
  Wire.onReceive(receiveEvent);
}
void loop(){
}

void receiveEvent(int howMany)
{

  if(Wire.available()){
    char z = Wire.read(); 
    Serial.print(z);
    
    if(z == 'a'){
      a = Wire.read();
      Serial.println(a);
    }
    if(z == 'b'){
      b = Wire.read();
      Serial.println(b);

      Var= a + (b * 256); // put the 8 bit and 16 bit numbers together to get the Unsiged Int again.

      Serial.println(Var);
    }
}
byte A;
byte B;

By convention, all capital letter names are reserved for constants. Constants NEVER appear on the left of an equal sign.

There is no reason for these two variables to be global.

    Wire.beginTransmission(1);
    Wire.write("a");
    Wire.write(A);
    Wire.endTransmission();
    Wire.beginTransmission(1);
    Wire.write("b");
    Wire.write(B);
    Wire.endTransmission();

Open the connect ONCE. Send ALL the data. Close the connection. There is no need to send "a" and "b".

Thanks for the answer!

By convention, all capital letter names are reserved for constants.

oke never seen that at any tutorial but its nice to know.

Constants NEVER appear on the left of an equal sign.

yes i know Constants are Read-only.

There is no reason for these two variables to be global.

yeah i need to find my way in this. but it just seem more logic to have all variable at one place.
as i sad "hobbyist"

Open the connect ONCE. Send ALL the data. Close the connection. There is no need to send "a" and "b".

yes i know but that the next step.
as everybody says Step for step.
and its the first time i use I2C, so its all new to me.