Question
I am porting some c# code to vb.net, currently trying to figure out how to do this..
byte isEndReached = //get some data
if (isEndReached != 0)
{
for (int y = 0; y < isEndReached ; y++)
{
//do some stuff
}
}
My attempt:
Dim isEndReached As Byte = ''//getsomedata
If Not isEndReached Is Nothing Then
For y As Byte = 0 To isEndReached - 1
''//do some stuff
Next
End If
Problem is I am getting the following error:
'Is' operator does not accept operands of type 'Byte'. Operands must be reference or nullable types.
How am I supposed to fix this?
Thanks!
Solution
You can't use Is
with value types. Likewise, Nothing
has a different meaning for value types than for reference types. You can just write it like this:
If isEndReached <> 0 Then
or like this:
If isEndReached <> Nothing Then
and looking at your code, I'd actually write it like this in case the function somehow returns a negative value for the byte:
If isEndReached > 0 Then
or alternatively declare your byte on the previous line and then just loop while it's less than isEndReached:
Dim y As Byte
While y < isEndReached
''...
y += 1
End While
Your For
doesn't have the exact same meaning as the C# code either, but it should actually be a better match- you're comparing bytes to bytes rather than ints to bytes.