C
C#4mo ago
stigzler

Something getting lost in C# to VB.net conversion

For reasons too annoying to go into, I'm having to convert some code from an original library written in c# to one written in vb.net. I've used an automatic code converter and whilst it compiles, I'm getting an error at run time on one particular line: c#:
dest[i] = (int)((uint)(newAlpha[i] << 24) | (uint)(newRed[i] << 16) | (uint)(newGreen[i] << 8) | (uint)newBlue[i]);
dest[i] = (int)((uint)(newAlpha[i] << 24) | (uint)(newRed[i] << 16) | (uint)(newGreen[i] << 8) | (uint)newBlue[i]);
vb:
dest(i) = CInt(CUInt(newAlpha(i) << 24) Or CUInt(newRed(i) << 16) Or CUInt(newGreen(i) << 8) Or CUInt(newBlue(i)))
dest(i) = CInt(CUInt(newAlpha(i) << 24) Or CUInt(newRed(i) << 16) Or CUInt(newGreen(i) << 8) Or CUInt(newBlue(i)))
Error: System.OverflowException: 'Arithmetic operation resulted in an overflow.' Code works fine in c#. I haven't used bitshifts in my code before, so struggling to deconstruct what the problem might be. Any ideas?
11 Replies
stigzler
stigzler4mo ago
(P.S. I also can't understand how a variable can be assigned to one value OR another)
mtreit
mtreit4mo ago
The C# code is using bitwise or operator, presumably the Or keyword in VB does the equivalent It's possible the VB project is using checked and the C# is using unchecked
mtreit
mtreit4mo ago
Looks like VB.NET doesn't have unchecked like C# does https://github.com/dotnet/vblang/issues/494
GitHub
[Proposal]: VB New Feature Unchecked math · Issue #494 · dotnet/vbl...
This is to open a "Feature Specification" following the process "roslyn/docs/contributing/Developing a Language Feature.md" for PR VB Prototype "Checked expressions" d...
mtreit
mtreit4mo ago
Whereas unchecked is the default in C#
reflectronic
reflectronic4mo ago
yeah, you need to change the option in your project file <RemoveIntegerChecks>true</RemoveIntegerChecks> i don't remember what it is called in the project properties window it is a global setting, indeed there is no way to apply it just to a few lines of code if this is some kind of environment where you don't control the compilation options (i can't imagine a good reason to be doing this otherwise) you are kinda screwed though
mtreit
mtreit4mo ago
Wondering why you can't keep the code as C# and have the VB code reference the C# project
reflectronic
reflectronic4mo ago
if that's an option i very strongly agree with doing that instead
stigzler
stigzler4mo ago
Sadly, not an option. Trying to integrate the existing c# code into a legacy vb library. I'm trying to render the final library as one dll, rather than two (the c# library is a single class). Let me look into RemoveIntegerChecks
reflectronic
reflectronic4mo ago
personally i would make sure i couldn't use something like ILMerge before resorting to this. it would allow you to combine the C# and VB outputs into one assembly
mtreit
mtreit4mo ago
What's wrong with two dlls?
stigzler
stigzler4mo ago
That did it reflectronic - brilliant - thanks. Thanks to both of you for the quick analysis!