none
Conversion issue from C# to VB RRS feed

  • Question

  • There is a line of code I have in C#:-

    var messageHeader = socketArgs.Buffer[0] * 100000000

                        + socketArgs.Buffer[1] * 1000000

                        + socketArgs.Buffer[2] * 10000

                        + socketArgs.Buffer[3] * 100

                        + socketArgs.Buffer[4];

    Which is translated as:-

    Dim messageHeader = socketArgs.Buffer(0) * 100000000 + socketArgs.Buffer(1) * 1000000 + socketArgs.Buffer(2) * 10000 + socketArgs.Buffer(3) * 100 + socketArgs.Buffer(4)

    Now the values of Buffer(0) mean that in VB it causes an error as basically it would be something like this:-

    Dim messageHeader As UInt32 = 43 * 100000000 + 43 * 1000000 + 43 * 10000 + 43 * 100 + 43

    Which gives:-

    Severity

    Code

    Description

    Project

    File

    Line

    Suppression State

    Error

    BC30439

    Constant expression not representable in type 'Integer'.

    CODE_TEST_APP

    C:\Users\darre\Documents\Visual Studio 2019\Projects\CODE_TEST_APP\CODE_TEST_APP\Form1.vb

    18

    Active

    or crashes the app with "Arithmetic operation resulted in an overflow.


    So I assume a difference in how C# handles the code compared to VB

    Any ideas please?


    Darren Rose

    Friday, October 4, 2019 8:36 PM

All replies

  • Sorry meant to say that the socketargs in above code are results from System.Net.Sockets.SocketAsyncEventArgs

    Darren Rose

    Friday, October 4, 2019 8:52 PM
  • You can do :

       Dim messageHeader As ULong = CULng(43 * 100000000UL + 43 * 1000000 + 43 * 10000 + 43 * 100 + 43)

    Friday, October 4, 2019 8:57 PM
  • Thanks, but unfortunately that doesn't help - still get "Arithmetic operation resulted in an overflow.


    Darren Rose

    Friday, October 4, 2019 9:24 PM
  • Thanks, but unfortunately that doesn't help - still get "Arithmetic operation resulted in an overflow.


    Darren Rose

    Hi

    That code from Castorix31 produces    4343434343   here


    Regards Les, Livingston, Scotland

    Friday, October 4, 2019 9:32 PM
  • Below is the sub in the code where this issue exists

    It crashes on the line - 

     Dim messageHeader = socketArgs.Buffer(0) * 100000000 + socketArgs.Buffer(1) * 1000000 + socketArgs.Buffer(2) * 10000 + socketArgs.Buffer(3) * 100 + socketArgs.Buffer(4)

    I tried it as Castorix31 suggested as well

    Either way gives same error

    Values of socketArgs.Buffer(0), (1), (2), (3) and (4) at time of crash is 43 hence my example

    Public Sub ProcessSocketArgs(ByVal socketArgs As SocketAsyncEventArgs, ByVal completedHandler As EventHandler(Of SocketAsyncEventArgs), ByVal socket As Socket)
    			Try
    				If socketArgs.BytesTransferred = 0 Then
    					If Config.Current.StartupMode = Config.StartupModes.Notifier Then
    						Environment.Exit(0)
    					End If
    					Return
    				End If
                    LastMessage = DateTime.Now
    
    
                    Dim messageHeader = socketArgs.Buffer(0) * 100000000 + socketArgs.Buffer(1) * 1000000 + socketArgs.Buffer(2) * 10000 + socketArgs.Buffer(3) * 100 + socketArgs.Buffer(4)
    
    
                    If AggregateMessages.Count = 0 AndAlso socketArgs.BytesTransferred - 5 = messageHeader Then
                        ProcessMessage(socketArgs.Buffer.Skip(5).Take(socketArgs.BytesTransferred - 5).ToArray())
                        Return
                    Else
                        If ExpectedBinarySize = 0 Then
    						ExpectedBinarySize = messageHeader
    					End If
    					AggregateMessages.AddRange(socketArgs.Buffer.Take(socketArgs.BytesTransferred))
    					Do While AggregateMessages.Count - 5 >= ExpectedBinarySize
    						ProcessMessage(AggregateMessages.Skip(5).Take(ExpectedBinarySize).ToArray())
    						AggregateMessages.RemoveRange(0, ExpectedBinarySize + 5)
    						If AggregateMessages.Count > 0 Then
    							ExpectedBinarySize = AggregateMessages(0) * 100000000 + AggregateMessages(1) * 1000000 + AggregateMessages(2) * 10000 + AggregateMessages(3) * 100 + AggregateMessages(4)
    						Else
    							ExpectedBinarySize = 0
    							AggregateMessages.Clear()
    						End If
    					Loop
    				End If
    			Catch ex As Exception
    				Utilities.WriteToLog(ex)
    				AggregateMessages.Clear()
    				ExpectedBinarySize = 0
    
                    Stop
    
                Finally
    				If socket IsNot Nothing AndAlso socket.Connected Then
    					If Not socket.ReceiveAsync(socketArgs) Then
    						completedHandler(socket, socketArgs)
    					End If
    				End If
    			End Try
    		End Sub


    Darren Rose

    Friday, October 4, 2019 9:35 PM
  • Okay TAKE 2 - think I need new glasses

    Admittedly I had missed that Castorix has put UL after the 100000000 e.g. 100000000UL - for some reason just read through it I think!

    Anyway still doesn't work in the code I am trying to convert as then gives same error a few lines later on the line

    ExpectedBinarySize = messageHeader

    Banging my head against a wall I think....

    Everything else converted fine from C# to VB and works, just not this part


    Darren Rose

    Friday, October 4, 2019 9:50 PM
  • Okay TAKE 2 - think I need new glasses

    Admittedly I had missed that Castorix has put UL after the 100000000 e.g. 100000000UL - for some reason just read through it I think!

    Anyway still doesn't work in the code I am trying to convert as then gives same error a few lines later on the line

    ExpectedBinarySize = messageHeader

    Banging my head against a wall I think....

    Everything else converted fine from C# to VB and works, just not this part


    Darren Rose

    Hi

    Is the Type of ExpectedBinarySize ULong?


    Regards Les, Livingston, Scotland

    Friday, October 4, 2019 10:10 PM
  • No it is Integer - or should I say was Integer, I now tried changing it to Ulong and it seems to get past that point, but it leads me on to another issue I seem to have with the conversion and that is the values getting returned by the SocketAsyncEventArgs.Buffer

    Running the C# version the values in buffer(0) - buffer(4) are 

    0, 0, 0, 0, 43

    but running converted code in VB the values in buffer(0) - buffer(4) are 

    43, 43, 43, 43, 43

    as per screenshots below

    So obviously something odd going on somewhere else in the code or SocketAsyncEventArgs works differently in C# than VB

    Have spend hours on this and going mad, not helped by the fact I am suffering from Diverticulitis so in extreme pain – this is doing my head in!!! - but so want to get it working in VB as I can learn more about the project in VB than I can C# as that is my preferred language 


    Darren Rose


    • Edited by wingers Friday, October 4, 2019 10:31 PM typo
    Friday, October 4, 2019 10:31 PM
  • The problem is that the original C# code is basing its logic on integer overflow, which C# allows by default. 

    The solution is to change your VB project options (Compile -> Advanced Compiler Options) so that the "Remove integer overflow checks" is checked. Also explicitly specify the type as "UInteger" (this is the original C# inferred type).  I've tested your example this way and achieved your original C# results.

    Even though this should solve the problem, I strongly recommend a more sane approach rewriting some code since basing logic on integer overflow seems just a bit crazy to me.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter





    Friday, October 4, 2019 11:44 PM
  • Okay thanks I will give that a try initially and then look into re-writing some of the code.  :)

    Actually to be fair the main issue is really going to be the second point I mentioned above about why getting differing values from SocketAsyncEventArgs.Buffer depending on whether running in VB or C# - that seems to be the main thing stopping me getting it working


    Darren Rose



    • Edited by wingers Saturday, October 5, 2019 12:02 AM
    Friday, October 4, 2019 11:55 PM
  • Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 8, 2019 9:16 AM
    Moderator
  • Do you resolve the issue? If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.


    Clearly got no more replies as you can see, hence not solved and no answer to mark as resolved

    Darren Rose

    Tuesday, October 8, 2019 10:25 AM
  • Hello,

    Any ideas please?

    'var' type is not an equivalent of UInt32.

    Read about types in .Net. 

    P.S. There also is strange multiplication 100(0) - didn't give any reasonable correction... probably missing 0x or 00...


    Sincerely, Highly skilled coding monkey.

    Tuesday, October 8, 2019 11:54 AM
  • Hello,

    Any ideas please?

    'var' type is not an equivalent of UInt32.

    Read about types in .Net. 

    P.S. There also is strange multiplication 100(0) - didn't give any reasonable correction... probably missing 0x or 00...


    Sincerely, Highly skilled coding monkey.

    Yes I know var is not an equivalent, hence asking on forum

    Can't see where you are seeing 100(0) ??


    Darren Rose

    Tuesday, October 8, 2019 11:59 AM
  • >No it is Integer 
    No, it is not.

    You took 5 bytes from buffer and try to put them into 4 bytes long UInt32... Ok - with multiplication correction, but it still longer than 32 bits...


    Sincerely, Highly skilled coding monkey.

    Tuesday, October 8, 2019 11:59 AM
  • >No it is Integer 
    No, it is not.

    You took 5 bytes from buffer and try to put them into 4 bytes long UInt32... Ok - with multiplication correction, but it still longer than 32 bits...


    Sincerely, Highly skilled coding monkey.

    I was asked "Is the Type of ExpectedBinarySize ULong?"

    I answered it was integer, because that is what is declared as in the code I had - hence my answer - so not sure why telling me No, it is not - I answered the question with what is shown in the code..


    Darren Rose

    Tuesday, October 8, 2019 12:03 PM
  • Hi

    Another tuppence worth. I suspect you do not have Option Strict set to True. I would suggest making sure it is set to True and corrections made where necessary in the code which would no doubt show a few.

    That Option which could help you identify some possible errors.


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, October 8, 2019 12:18 PM
    Tuesday, October 8, 2019 12:18 PM
  • >>Can't see where you are seeing 100(0) ??

    100 and zeros in period.


    Sincerely, Highly skilled coding monkey.

    Tuesday, October 8, 2019 12:21 PM
  • Hi

    Another tuppence worth. I suspect you do not have Option Strict set to True. I would suggest making sure it is set to True and corrections made where necessary in the code which would no doubt show a few.

    That Option which could help you identify some possible errors.


    Regards Les, Livingston, Scotland


    Thanks, just tried that and 100's of errors, so I will work through and see if that helps

    Darren Rose

    Tuesday, October 8, 2019 12:25 PM
  • >>Can't see where you are seeing 100(0) ??

    100 and zeros in period.


    Sincerely, Highly skilled coding monkey.

    No idea what you are trying to say with short cryptic responses, if you are trying to help then please elaborate so I understand and can answer properly

    Darren Rose

    Tuesday, October 8, 2019 12:26 PM
  • ​Не переживай - для тебя это не существенно.

    Sincerely, Highly skilled coding monkey.

    Tuesday, October 8, 2019 12:32 PM
  • Hello,

    Any ideas please?

    'var' type is not an equivalent of UInt32.

    Read about types in .Net. 

    P.S. There also is strange multiplication 100(0) - didn't give any reasonable correction... probably missing 0x or 00...


    Sincerely, Highly skilled coding monkey.

    Andrej

    The Var keyword in C# is the same as Dim with option Infer on in VB. It was added to C# when it was needed with Linq. 

    The VB Var (variable) keyword has no equivalent in C#. 

    Therefore the code 

     Dim X = CUInt(10000)

    Results in a dimension from the  Uint type

    Of course can Darren also use that Conversion function in his code as well to try if it solves something.


    Success
    Cor



    Tuesday, October 8, 2019 4:29 PM
  • Thanks for clarifying Cor, was starting to get more confused over who was saying what.  Had assumed as his signature said "Highly skilled coding monkey." he knew what he was talking about, but in the end just confused me more and wouldn't explain his questions fully.

    Thanks


    Darren Rose

    Tuesday, October 8, 2019 4:37 PM
  • It's very difficult to deal with thoose who didn't try to understand what was given.

    Maximum value of Int32 is:

            2,147,483,647

    first position of your multiplication - 43 * 100.000.000 - gives: 

            4,300,000,000

    declaration of the result as UInt32 didn't resolve a problem - max UInt32 is

            4,294,967,295

    But your overflow happen earlier - when you multiplay (Int32)43 * (Int32)100.000.000 - it didn't fit Int32... If you change type to UInt32 - this will not resolve the problem - variable still too small to handle the result.

    You could use Int64 or UInt64 (UL, ULong) to perform calculations, but result will be truncated when you put it in UInt32...

    I do not look on the protocol which handled by this code, but would assume that in the buffer are expected something like two-digits-per-byte-decimally-corrected UInt32 value. In this case you would never get your 

    43, 43, 43, 43, 43

    maximum what you may have in this bytes will be:

    42, 94, 96, 72, 95

    So, there will be no overflow in real world if you use UInt32... 


    Sincerely, Highly skilled coding monkey.

    Wednesday, October 9, 2019 8:41 AM
  • >The VB Var (variable) keyword has no equivalent in C#. 

    Thnks - I work with both languages.


    Sincerely, Highly skilled coding monkey.

    Wednesday, October 9, 2019 8:47 AM
  • It is clear that getting this code working in VB is above my ability.  Thank you for all your replies, please consider this post closed

    Darren Rose

    Wednesday, October 9, 2019 9:29 AM