0

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!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • If you are porting code, why are you changing it? The original code verified that isChecked was different than zero, but you changed it to compare it with Nothing.. Also, the type of the variable "y" changed (int => byte). Any reasons for these changes?? – Meta-Knight Aug 02 '09 at 23:53

1 Answers1

2

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thank you! I'm using If isEndReached <> 0 Then and it works just as I wanted. –  Aug 03 '09 at 00:03