Conversion to byte

R

Robert Scheer

Hi.

I am trying to add a value to a byte property as below:

Utilization.Limit +=1

This gives me a message saying that Option Strict On disallows
implicit conversions from 'Integer' to 'Byte'.

I changed my code to:

Utilization.Limit += Convert.ToByte(1)

And now it is working. But is this correct? Or is there a better way
to increment my Byte property?

Regards,
Robert Scheer
 
A

Armin Zingler

Am 05.07.2010 18:28, schrieb Robert Scheer:
Hi.

I am trying to add a value to a byte property as below:

Utilization.Limit +=1

This gives me a message saying that Option Strict On disallows
implicit conversions from 'Integer' to 'Byte'.

I changed my code to:

Utilization.Limit += Convert.ToByte(1)

And now it is working. But is this correct? Or is there a better way
to increment my Byte property?

Utilization.Limit += CByte(1)

As CByte is a keyword, not a function, the value is compiled as
a byte, not converted at runtime. Though, your version is ok, too.
 
A

Armin Zingler

Am 05.07.2010 22:15, schrieb Onur Güzel:
Yep, but CByte is mentioned as "function" on MSDN which you call it as
keyword:

http://msdn.microsoft.com/en-us/library/2ssb79wt(VS.85).aspx

VBScript documentation? :)

Well, it's used like a function but what the compiler does
depends on the type of the expression. It's not necessarily
a function call.
Acturally Cxxx (and CByte) is conversion operator.

Or "conversion function": ;-)
http://msdn.microsoft.com/en-us/library/s2dy91zy.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top