Click here to Skip to main content
15,566,522 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I'm converting a set of IO control module programs from C# to VB.Net.
All the work is almost completed, but only stuck in this line of calculation formula.

C#
byte[] src = new byte[10+(8-1)/8];

VB.NET
Dim src As Byte() = New Byte(10 + (8 - 1) / 8 - 1) {}

In C#, I got the length of src is 10. But in vb.net, no matter how to combine the formula, the length of src is 11 or 12.
How can I get the same length of src in VB.Net? Thanks!

What I have tried:

I have tried to use converter.telerik.com to convert to vb.net, but no matter how I modify it, the result is just different from C#.
Posted
Updated 17-Nov-21 2:13am
Comments
Richard MacCutchan 17-Nov-21 8:56am    
Why not just write:
Dim src As Byte() = New Byte(10)

?
Hu Christian 17-Nov-21 10:38am    
Actually, it will bring in a variable. I simplified the formula to make my question clearer. Thanks.
Dim src As Byte() = New Byte(10 + (Var - 1) \ 8 - 1) {}

In your C# code, you're using integer division: (8 - 1) / 8 === 7 / 8 === 0, giving you 10 + 0 === 10 elements.

In VB.NET, you're using floating-point division: (8 - 1) / 8 === 7 / 8 === 0.875; VB then rounds that number up, giving you 10 + 1 === 11 elements.

If you want to perform integer division in VB.NET, use the \ operator instead:
VB.NET
Dim src As Byte() = New Byte(10 + (8 - 1) \ 8 - 1) {}

\ Operator - Visual Basic | Microsoft Docs[^]
 
Share this answer
 
v3
Comments
Hu Christian 17-Nov-21 7:47am    
You solve my question. Thank you for prompt reply.
Possibly it should be:
VB
Dim src As Byte() = New Byte(10 + (8 - 1) \ 8 - 1) {}

See: \ Operator - Visual Basic | Microsoft Docs[^].
 
Share this answer
 
Comments
Hu Christian 17-Nov-21 7:47am    
Thank you!
CPallini 17-Nov-21 8:06am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900