Author Topic: Casting Integer to Byte  (Read 286 times)

Bartw

  • Newbie
  • *
  • Posts: 29
    • View Profile
Casting Integer to Byte
« on: January 12, 2017, 09:22:45 pm »
I am trying to dump an I2C Dump function.

I have a for loop which has to be integer (scripting limitation) and inside i have call a I2C read function which use Byte for the address.
I can't seem to cast a integer to a byte with and the ToByte() mentioned function in the manual doesn't seem to work??

Regards
Bart

Share on Facebook Share on Twitter


mc-Josh

  • Global Moderator
  • Newbie
  • *****
  • Posts: 27
    • View Profile
Re: Casting Integer to Byte
« Reply #1 on: January 13, 2017, 03:55:42 pm »
You have to first create a byte variable and then cast it. The code below will cast the intNum as Byte to byteNum

Code: [Select]
Dim intNum As Integer = 44
Dim byteNum As Byte = 0
       
byteNum = intNum.ToByte()

If this doesn't answer your problem please post your code.

Josh

Bartw

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Casting Integer to Byte
« Reply #2 on: January 16, 2017, 04:22:12 pm »
Thank you was a syntax issue.
Had assumed the following from documentation.. I normally don't do object.. :)
       
byteNum = ToByte(intNum)

mc-John

  • Global Moderator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: Casting Integer to Byte
« Reply #3 on: January 17, 2017, 09:42:40 am »
It is documented at chapter 16.5. You have to think in objects though. All functions work on an object, in this case an integer. If you get used to this it is so much nicer than procedural programming where functions are not connected to an object.