How do I increment a byte with out casting?

K

kosherpiggy

I want to increment a byte when I have Option Strict on and I don't
want to do any casting. The code below does it but it is hard on the
eyes.

Dim lineNumber As Byte = 0
Dim incrementer As Byte = 1

For Each line As Line In mCollection
lineNumber += incrementer
line.LineNumber.Value = lineNumber
Next

I'd like to do something more like this

Dim lineNumber As Byte = 0

For Each line As Line In mCollection
lineNumber += 1y
line.LineNumber.Value = lineNumber
Next

Where y is what ever suffix would create that nameless temporary object
as a byte with the value of 1.

Is there a way to do this? Is there a better way to increment a byte?

Thanks,
Russ
 
C

Cor Ligthert [MVP]

Hi,

You make me curious, why are you using a processor cycles eating command
with a byte for this kind of operations and not the normal Integer that fits
directly in the accumulatorregisters from a 32bit computer.

Cor
 
R

Russ

I honestly couldn't tell you. I'm working at a new place that is
dragging along a lot of converted VB6 code and I assume old databases;
not that that justifies using a byte. I only mean to explain the
environment I seem to be stuck in. When I've been here a while they
might answer my asking that question with a "Fine you're so smart
go fix it".

Regardless, is there any way to increment a byte that doesn't look so
goofy?

Russ
 
H

Herfried K. Wagner [MVP]

I want to increment a byte when I have Option Strict on and I don't
want to do any casting.

You don't need to do any explicit casting!
 
C

C-Services Holland b.v.

I want to increment a byte when I have Option Strict on and I don't
want to do any casting. The code below does it but it is hard on the
eyes.

Dim lineNumber As Byte = 0
Dim incrementer As Byte = 1

For Each line As Line In mCollection
lineNumber += incrementer
line.LineNumber.Value = lineNumber
Next

I'd like to do something more like this

Dim lineNumber As Byte = 0

For Each line As Line In mCollection
lineNumber += 1y
line.LineNumber.Value = lineNumber
Next

Where y is what ever suffix would create that nameless temporary object
as a byte with the value of 1.

Is there a way to do this? Is there a better way to increment a byte?

Thanks,
Russ

Why not just use

lineNumber += 1

What's the problem with that?
 
R

Russ

Why not just use

lineNumber += 1

What's the problem with that?

The problem is just typing 1 gives you an integer. If lineNumber is a
Byte you're asking VB to do a narrowing conversion cast. When option
strict is on that line produces this error message:

"Option Strict On disallows implicit conversions from 'Integer' to
'Byte'"

I need to create 1 as a byte but I'd like to do it with out having to
create a variable or an explicit cast. I need strict on to enforce
type safety.
 
R

Russ

Herfried said:
You don't need to do any explicit casting!

Great! Care to tell me how to do it? Don't forget I said I don't want
to do ANY casting implicit or explicit. Implicit won't work with
Option Strict on.

"Option Strict On disallows implicit conversions from 'Integer' to
'Byte'"

I'm hoping I can create a nameless temporary Byte with a value of 1.
I've seen this kind of thing done with suffixes.
 
H

Herfried K. Wagner [MVP]

Russ said:
The problem is just typing 1 gives you an integer. If lineNumber is a
Byte you're asking VB to do a narrowing conversion cast. When option
strict is on that line produces this error message:

"Option Strict On disallows implicit conversions from 'Integer' to
'Byte'"

I am not able to repro that in VB.NET 2003. Are you using VB 2005?
I need to create 1 as a byte but I'd like to do it with out having to
create a variable or an explicit cast. I need strict on to enforce
type safety.

You'll have to use 'CByte(x)' because there are no type characters for the
'Byte' type.
 
C

Cor Ligthert [MVP]

Herfried,
You'll have to use 'CByte(x)' because there are no type characters for the
Byte' type.

Which is converting but by C people called casting.

You know that song mostly done by Harry Belefonte, "there is a hole in the
bucket"

Cor
 
R

Russ

Herfried said:
I am not able to repro that in VB.NET 2003. Are you using VB 2005?
From Help About I see:
Microsoft Development Environment 2003 Version 7.1.3088
Microsoft .NET Framework 1.1 Version 1.1.4322

Installed Products:
Microsoft Visual Basic .NET 69461-005-0669635-18793
-Snip-
Microsoft Visual Studio .NET 2003 Hotfix (KB822690)
 
R

Russ

Herfried said:
You'll have to use 'CByte(x)' because there are no type characters for the
'Byte' type.

This is what I was afraid of when I couldn't find a suffix for Byte.
Thanks for confirming it.

One last question: Which of these do you think is the least horrifying
practice?

Dim LineNumber as Byte = 0
Dim Incrementer as Byte = 1

LineNumber += Incrementer

- or -

Dim LineNumber as Byte = 0
LineNumber += Cbyte(1)

Short of abolishing all bytes that ever need incrementing these seem to
be my only options under option strict.

Thanks for all your help,
Russ
 
H

Herfried K. Wagner [MVP]

Russ said:
This is what I was afraid of when I couldn't find a suffix for Byte.
Thanks for confirming it.

One last question: Which of these do you think is the least horrifying
practice?

Dim LineNumber as Byte = 0
Dim Incrementer as Byte = 1

LineNumber += Incrementer

- or -

Dim LineNumber as Byte = 0
LineNumber += Cbyte(1)

Short of abolishing all bytes that ever need incrementing these seem to
be my only options under option strict.

I believe that 'LineNumber += 1' is the best solution. It will perform the
conversion implicitly and no compile-time warning is raised.
 
H

Herfried K. Wagner [MVP]

Russ said:

Sorry, you are right, 'LineNumber += 1' will raise a compile-time error,
however for some reason this was not the case for my sample project although
'Option Strict' was turned on. So I'd vote for 'CByte'.
 
R

Russ

Herfried said:
Russ said:

Sorry, you are right, 'LineNumber += 1' will raise a compile-time error,
however for some reason this was not the case for my sample project although
'Option Strict' was turned on. So I'd vote for 'CByte'.

Well now you've got me wondering how your sample project was
configured. :) Anyway, I really appreciate the help. I'm new to VB
and I'm trying to make sure my coding style develops into a good one.
 
H

Herfried K. Wagner [MVP]

Russ said:
Well now you've got me wondering how your sample project was
configured. :) Anyway, I really appreciate the help. I'm new to VB
and I'm trying to make sure my coding style develops into a good one.

Honestly I don't know. It took a few minutes until the blue squiggly line
became visible.
 

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