Answered by:
Equivalent of "Unchecked" keyword \expression in VB.NET

Question
-
Hi,
I have converted the following C# code to VB.NET.But I am unable to find the equivalent of C# "Unchecked" keyword in VB.NET.Below is the C# Code and following the C# Code is converted VB Code.After conversion and building the VB Code I am getting the error : "Constant expression not representable in type 'UInteger'."
C#:
if (milliseconds == unchecked((uint)Timeout.Infinite)) { return TimeSpan.MaxValue; }
Converted VB.NET:
If milliseconds = (CUInt(System.Threading.Timeout.Infinite)) Then Return System.TimeSpan.MaxValue End if
Please help me out ASAP.
Thanks,
Vipin
- Moved by Rudedog2 Thursday, April 8, 2010 2:25 PM Visual Basic Issue (From:Visual C# General)
Thursday, April 8, 2010 1:33 PM
Answers
-
Check this option, not exact what you are trying but related
http://msdn.microsoft.com/en-us/library/key2x70f(VS.71).aspx
Arjun Paudel- Marked as answer by Jeff Shan Thursday, April 15, 2010 3:28 AM
Saturday, April 10, 2010 5:56 AM -
Your C# code is just a bit out of context. Please post the entire method.
The original code is not making sense. It is casting a 32-bit integer constant to an an unsigned 32-bit integer. The value of the constant is -1. What is the point in cast an integer constant that will always thrown an exception? The code generates the following compiler warning.
" Warning - Comparison to integral constant is useless; the constant is outside the range of type 'int' "
The unchecked keyword in C# is used to disable run time exceptions being thrown on integer overflows when performing calculations. There are some circumstances where the overflow is of no concern. This ain't one of them.
There is no direct equivalent keyword in Visual Basic. In VB you must perform the risky calculation inside of a Try/Catch/Finally block.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."- Proposed as answer by Cor Ligthert Saturday, April 10, 2010 6:35 AM
- Marked as answer by Jeff Shan Thursday, April 15, 2010 3:28 AM
Thursday, April 8, 2010 2:24 PM -
Hi Vipins,
I would not worry about the keyword unchecked from C# in this scenario as you are not attempting to change the value
passed to the Function, within the Function itself . :-)
As forum user Rudy a.k.a. RudeDog2 has said, there is no equivalent in Vb.Net.
The constant System.Threading.Timeout.Infinite has a value of -1 according to.>>
http://msdn.microsoft.com/en-us/library/system.threading.timeout.infinite (VS.85).aspx
so using a UInt or unsigned Integer of any size does not make any sense as they all have a minimum value = 0
Anyway, I believe C# Public Static equates to a Public Shared method in Vb.Net so my best attempt at conversion
and writing a Function that should not create an error is ( I added an overloaded version in case you want to pass a negative Integer).>>
Public Shared Function FromMilliseconds(ByVal milliSeconds As UInteger) As TimeSpan If milliSeconds = System.Threading.Timeout.Infinite Then Return TimeSpan.MaxValue ElseIf milliSeconds >= 0 And milliSeconds <= TimeSpan.MaxValue.TotalMilliseconds Then Return TimeSpan.FromMilliseconds(milliSeconds) End If End Function 'Overloaded version in case you want to pass a negative value.>> Public Shared Function FromMilliseconds(ByVal milliSeconds As Integer) As TimeSpan If milliSeconds = System.Threading.Timeout.Infinite Then Return TimeSpan.MaxValue ElseIf milliSeconds >= TimeSpan.MinValue.TotalMilliseconds And milliSeconds <= TimeSpan.MaxValue.TotalMilliseconds Then Return TimeSpan.FromMilliseconds(milliSeconds) End If End Function
Regards,
John
Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4- Marked as answer by Jeff Shan Thursday, April 15, 2010 3:28 AM
Saturday, April 10, 2010 2:23 AM
All replies
-
try this -
if (milliseconds == ((uint)System.Threading.Timeout.Infinite))
{
return System.TimeSpan.MaxValue;
}jayant
Thursday, April 8, 2010 1:41 PM -
Hi Jayant,
Thanks...But this is for C#...i want it in VB.Net.
Waiting..
Thursday, April 8, 2010 1:51 PM -
ok just remove threading
try this
If milliseconds = CUInt(Timeout.Infinite) Then
Return TimeSpan.MaxValue
End Ifand inform me it is working or not.
jayant
Thursday, April 8, 2010 1:55 PM -
if above code is not working then
try this.
If milliseconds = Convert.ToUInt16(Timeout.Infinite) Then
Return TimeSpan.MaxValue
End Ifjayant
Thursday, April 8, 2010 2:03 PM -
Hi Jayant,
I tried this....but on 'Timeout.Infinite' it's giving error 'Constant expression not representable in type 'UInteger'
If milliseconds = CUInt(Timeout.Infinite) Then Return TimeSpan.MaxValue End If
Plz let me know...
Thursday, April 8, 2010 2:05 PM -
try this.
If milliseconds = Convert.ToUInt16(Timeout.Infinite) Then
Return TimeSpan.MaxValue
End Ifjayant
Thursday, April 8, 2010 2:10 PM -
Hi,
For your information,below is the actual complete function in C# to be converted to VB.Net
public static TimeSpan FromMilliseconds(uint milliseconds) { if (milliseconds == unchecked((uint)Timeout.Infinite)) { return TimeSpan.MaxValue; } else { return TimeSpan.FromMilliseconds(milliseconds); } }
...Thanks..Waiting...Thursday, April 8, 2010 2:12 PM -
Your C# code is just a bit out of context. Please post the entire method.
The original code is not making sense. It is casting a 32-bit integer constant to an an unsigned 32-bit integer. The value of the constant is -1. What is the point in cast an integer constant that will always thrown an exception? The code generates the following compiler warning.
" Warning - Comparison to integral constant is useless; the constant is outside the range of type 'int' "
The unchecked keyword in C# is used to disable run time exceptions being thrown on integer overflows when performing calculations. There are some circumstances where the overflow is of no concern. This ain't one of them.
There is no direct equivalent keyword in Visual Basic. In VB you must perform the risky calculation inside of a Try/Catch/Finally block.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."- Proposed as answer by Cor Ligthert Saturday, April 10, 2010 6:35 AM
- Marked as answer by Jeff Shan Thursday, April 15, 2010 3:28 AM
Thursday, April 8, 2010 2:24 PM -
Hi Vipins,
I would not worry about the keyword unchecked from C# in this scenario as you are not attempting to change the value
passed to the Function, within the Function itself . :-)
As forum user Rudy a.k.a. RudeDog2 has said, there is no equivalent in Vb.Net.
The constant System.Threading.Timeout.Infinite has a value of -1 according to.>>
http://msdn.microsoft.com/en-us/library/system.threading.timeout.infinite (VS.85).aspx
so using a UInt or unsigned Integer of any size does not make any sense as they all have a minimum value = 0
Anyway, I believe C# Public Static equates to a Public Shared method in Vb.Net so my best attempt at conversion
and writing a Function that should not create an error is ( I added an overloaded version in case you want to pass a negative Integer).>>
Public Shared Function FromMilliseconds(ByVal milliSeconds As UInteger) As TimeSpan If milliSeconds = System.Threading.Timeout.Infinite Then Return TimeSpan.MaxValue ElseIf milliSeconds >= 0 And milliSeconds <= TimeSpan.MaxValue.TotalMilliseconds Then Return TimeSpan.FromMilliseconds(milliSeconds) End If End Function 'Overloaded version in case you want to pass a negative value.>> Public Shared Function FromMilliseconds(ByVal milliSeconds As Integer) As TimeSpan If milliSeconds = System.Threading.Timeout.Infinite Then Return TimeSpan.MaxValue ElseIf milliSeconds >= TimeSpan.MinValue.TotalMilliseconds And milliSeconds <= TimeSpan.MaxValue.TotalMilliseconds Then Return TimeSpan.FromMilliseconds(milliSeconds) End If End Function
Regards,
John
Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4- Marked as answer by Jeff Shan Thursday, April 15, 2010 3:28 AM
Saturday, April 10, 2010 2:23 AM -
Check this option, not exact what you are trying but related
http://msdn.microsoft.com/en-us/library/key2x70f(VS.71).aspx
Arjun Paudel- Marked as answer by Jeff Shan Thursday, April 15, 2010 3:28 AM
Saturday, April 10, 2010 5:56 AM -
Check this option, not exact what you are trying but related
http://msdn.microsoft.com/en-us/library/key2x70f(VS.71).aspx
Arjun PaudelExcellent suggestion.
Be aware of the primary difference between this option and the C# keyword. This option will apply to the entire assembly, while the keyword applies to just the block of code that it surrounds.
The keyword is used when the remainder from the overflow is of interest.
Mark the best replies as answers. "Fooling computers since 1971."Sunday, April 11, 2010 3:56 PM -
i see that no one really answered your question correctly. i had a similar question and here's what i found. VB and C# both let you set the entire project as checking for overflow or not checking for overflow. But that's not what i was looking for. Vipins in your case, you just need to know what C# does with -1 when converting to an unsigned value: the Max Unsigned value is the result. So your equivalent VB code If statement would be:
' System.Threading.Timeout.Infinite = -1, so its UInt32 equivalent = UInt32.MaxValue
If milliseconds = UInt32.MaxValue Then Return System.TimeSpan.MaxValue
It is that simple. When converting C# `checked` or `unchecked` keywords to VB.NET, if you still want simple and clean VB.NET code, then you really need to realize what is happening and recode it a bit differently. But you can always use a larger int datatype and then truncate the int down to the size you want. Or you can use BitConverter to convert one value to Bytes then back to the int type that you want. The BitConverter type would look like this:
If milliseconds = BitConverter.ToUInt32(BitConverter.GetBytes(System.Threading.Timeout.Infinite), 0) Then Return System.TimeSpan.MaxValue
Personally, i prefer the first snippet. But suit yourself.
- Edited by Humilulo Tuesday, March 3, 2015 10:26 PM
Tuesday, March 3, 2015 10:20 PM -
i see that no one really answered your question correctly. i had a similar question and here's what i found. VB and C# both let you set the entire project as checking for overflow or not checking for overflow. But that's not what i was looking for. Vipins in your case, you just need to know what C# does with -1 when converting to an unsigned value: the Max Unsigned value is the result. So your equivalent VB code If statement would be:
' System.Threading.Timeout.Infinite = -1, so its UInt32 equivalent = UInt32.MaxValue
If milliseconds = UInt32.MaxValue Then Return System.TimeSpan.MaxValue
It is that simple. When converting C# `checked` or `unchecked` keywords to VB.NET, if you still want simple and clean VB.NET code, then you really need to realize what is happening and recode it a bit differently. But you can always use a larger int datatype and then truncate the int down to the size you want. Or you can use BitConverter to convert one value to Bytes then back to the int type that you want. The BitConverter type would look like this:
If milliseconds = BitConverter.ToUInt32(BitConverter.GetBytes(System.Threading.Timeout.Infinite), 0) Then Return System.TimeSpan.MaxValue
Personally, i prefer the first snippet. But suit yourself.
Hello, be aware that this thread is five years old and nobody has asked this question in a very long time. Best to answer questions that are 30 days old or less.Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
Tuesday, March 3, 2015 10:30 PM