I’ve got a function which needs to take a UInt as a parameter. I'm using the .Net Micro Framework
As a simple example, I’d like to do something like:
Dim x As UInteger
For x = 0 To 100
Port.SetDutyCycle(x)
Thread.Sleep(10)
Next
For x = 100 To 0 Step -1
Port.SetDutyCycle(x)
Thread.Sleep(10)
Next
The second For won’t compile as -1 can’t be represented as a UInt. So next I tried…
For y As UInteger = 0 To 100
Port.SetDutyCycle(100 - y)
Thread.Sleep(10)
Next
This won’t compile as the - y forces a conversion to Long which then can’t be narrowed to UInt. Next attempt:
Port.SetDutyCycle(DirectCast(100 - y, UInteger))
The 100 - y won’t compile as “Value of type ‘Integer’ cannot be converted to ‘UInteger'”
So… How can I easily get a UInteger that decrements from 100 to 0 in a loop?
As Per Hans Passant’s answer, the 2nd code block works perfectly in vanilla .Net
You simply forgot to declare y. Fix:
The warning is generated because you have non-standard compile settings. Project + Properties, Compile tab, Warning configuration section. The normal setting for the “Implicit conversion” option is None. Leaving it set to Warning is fine, you just need to make the conversion explicit: