473,486 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I increment a byte with out casting?

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

Jun 6 '06 #1
14 4330
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

<ko*********@hotmail.com> schreef in bericht
news:11**********************@i40g2000cwc.googlegr oups.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

Jun 6 '06 #2
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:
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

<ko*********@hotmail.com> schreef in bericht
news:11**********************@i40g2000cwc.googlegr oups.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


Jun 6 '06 #3
<ko*********@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/>
Jun 6 '06 #4
ko*********@hotmail.com wrote:
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
Jun 7 '06 #5
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.

Jun 7 '06 #6

Herfried K. Wagner [MVP] wrote:
<ko*********@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.

Jun 7 '06 #7
"Russ" <ko*********@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/>

Jun 7 '06 #8
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:Oi**************@TK2MSFTNGP04.phx.gbl... "Russ" <ko*********@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/>

Jun 7 '06 #9

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)

Jun 7 '06 #10

Herfried K. Wagner [MVP] wrote:
"Russ" <ko*********@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

Jun 7 '06 #11
"Russ" <ko*********@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/>

Jun 7 '06 #12
"Russ" <ko*********@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/>

Jun 7 '06 #13
Herfried K. Wagner [MVP] wrote:
"Russ" <ko*********@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.

Jun 8 '06 #14
"Russ" <ko*********@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/>

Jun 8 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
26882
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
98
14298
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
21
14879
by: deepak | last post by:
hi folks I have a pointer to a file. If i increment the pointer, where will it point. I could not find the problem in the FAQs. Thanks in advance Deepak garg
6
16001
by: John Hoffman | last post by:
Reading registry: .... RegistryKey rksub = rkey.OpenSubKey(s); String valstr = rksub.GetValueNames(); foreach (String vs in valstr) { String vstr = rksub.GetValue(vs).ToString(); OR String...
3
54203
by: glenn | last post by:
I have a COM server that is returning a type Object that I need to cast as a byte array. Can anyone tell me how this is performed in C#? Thanks, glenn
3
1529
by: Fireangel | last post by:
I want to cast a class into a byte array. I've seen some examples of this floating around, but they all have simple data members. What happens if I cast something that has a ArrayList or an...
5
2210
by: Pohihihi | last post by:
Hello All, While replying to my post many lost the track of what I was asking but it was nice to know few extra things. Well coming back to my problem -- My field is varbinary in SQL db. Do you...
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.