Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Did you know that...

.... that when you execute CByte(True) you will get... 255(!!!) instead
of -1??? I have discovered this by chance when I was writing code like
this:

aByteVariable = aByteVariable -
CByte(aFunctionThatReturnsBoolean(anArgument) )

to get rid of the If-Then-Else clause. Thank God I was using a byte
variable and got an overflow error
for if I had used a long variable, I would probably never have
discovered the cause of the error...
Well, I'd have eventually but it would have taken more time, I
presume.

Darek
ps. Always step through your code line by line and check (almost)
every possible scenario.
If you do not do this, you can be very much taken by surprise :)

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 76
Default Did you know that...

Hi Darek

Byte in VBA is an unsigned data-type, so I presume it could never have
returned -1. I definitely agree with your closing comments though :-D

Best regards

Richard


On 27 Feb, 13:00, "darlove" wrote:
... that when you execute CByte(True) you will get... 255(!!!) instead
of -1??? I have discovered this by chance when I was writing code like
this:

aByteVariable = aByteVariable -
CByte(aFunctionThatReturnsBoolean(anArgument) )

to get rid of the If-Then-Else clause. Thank God I was using a byte
variable and got an overflow error
for if I had used a long variable, I would probably never have
discovered the cause of the error...
Well, I'd have eventually but it would have taken more time, I
presume.

Darek
ps. Always step through your code line by line and check (almost)
every possible scenario.
If you do not do this, you can be very much taken by surprise :)



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Did you know that...

Not surprising since -1 is not a possible byte value. Note that
CBool(CByte(True)) = True as expected. You did stumble on a lacuna in
the documentation which says that True is converted to -1 when you
convert to other data types.

Your suggestion of debugging carefully is good - it would save me a
lot of grief if I carried it out more consistently - but I suspect
that checking (almost) every possible scenario is impossible (for
combinatorial reasons - too many excecution paths and/or inputs!) in
all but the smallest program - which is why error trapping is a good
idea in order to catch the error scenarios you didn't think of.

-John

On Feb 27, 8:00 am, "darlove" wrote:
... that when you execute CByte(True) you will get... 255(!!!) instead
of -1??? I have discovered this by chance when I was writing code like
this:

aByteVariable = aByteVariable -
CByte(aFunctionThatReturnsBoolean(anArgument) )

to get rid of the If-Then-Else clause. Thank God I was using a byte
variable and got an overflow error
for if I had used a long variable, I would probably never have
discovered the cause of the error...
Well, I'd have eventually but it would have taken more time, I
presume.

Darek
ps. Always step through your code line by line and check (almost)
every possible scenario.
If you do not do this, you can be very much taken by surprise :)



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Did you know that...

Well, I agree on slight reflection. But you know...
after you have written several hundreds of lines of code
you definitely do not seem to remember all the ins and outs of
all the aspects of programming (tired?). Most likely, if I had
pondered over the issue for some time, I'd have come
to the same conclusion as you have. But anyway,
they say that True = -1 and this is what I have always
had in my mind. It was hard to forsee, after all, that
for bytes it would not work that well.... :) Good to know,
though, that there is a perfectly valid explanation of
what has happened.

Regs
Darek

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:55 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"