Home All Groups Group Topic Archive Search About

How do I increment a byte with out casting?

Author
6 Jun 2006 5:11 PM
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

Author
6 Jun 2006 7:44 PM
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

<kosherpi***@hotmail.com> schreef in bericht
Show quoteHide quote
news:1149613900.634596.223270@i40g2000cwc.googlegroups.com...
>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
>
Author
6 Jun 2006 9:00 PM
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



Cor Ligthert [MVP] wrote:
Show quoteHide quote
> 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
>
> <kosherpi***@hotmail.com> schreef in bericht
> news:1149613900.634596.223270@i40g2000cwc.googlegroups.com...
> >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
> >
Author
6 Jun 2006 9:19 PM
Herfried K. Wagner [MVP]
<kosherpi***@hotmail.com> schrieb:
>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!

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 Jun 2006 2:23 PM
Russ
Herfried K. Wagner [MVP] wrote:
> <kosherpi***@hotmail.com> schrieb:
> >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!
>

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.
Author
7 Jun 2006 11:31 AM
C-Services Holland b.v.
kosherpi***@hotmail.com wrote:
Show quoteHide quote
> 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?

--
Rinze van Huizen
C-Services Holland b.v
Author
7 Jun 2006 2:12 PM
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.
Author
7 Jun 2006 2:38 PM
Herfried K. Wagner [MVP]
"Russ" <kosherpi***@hotmail.com> schrieb:
>> 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 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 Jun 2006 3:25 PM
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

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:Oih0HAkiGHA.1600@TK2MSFTNGP04.phx.gbl...
> "Russ" <kosherpi***@hotmail.com> schrieb:
>>> 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 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.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
7 Jun 2006 4:08 PM
Russ
Herfried K. Wagner [MVP] wrote:

>> "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?

>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)
Author
7 Jun 2006 4:31 PM
Russ
Herfried K. Wagner [MVP] wrote:
> "Russ" <kosherpi***@hotmail.com> schrieb:

> > 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.
>

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
Author
7 Jun 2006 9:36 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Russ" <kosherpi***@hotmail.com> schrieb:
>> > 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.
>
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 Jun 2006 9:38 PM
Herfried K. Wagner [MVP]
"Russ" <kosherpi***@hotmail.com> schrieb:
> [...]

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'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 Jun 2006 12:06 AM
Russ
Herfried K. Wagner [MVP] wrote:
> "Russ" <kosherpi***@hotmail.com> schrieb:
> > [...]
>
> 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.
Author
8 Jun 2006 12:35 AM
Herfried K. Wagner [MVP]
"Russ" <kosherpi***@hotmail.com> schrieb:
>> 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.

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

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>