views:

210

answers:

3

I have problems in converting the following code-snippet from C# to VB.Net:

if ((currentItem.Tag as FileSystemKind?) != FileSystemKind.File)
   {
    if (currentFileName == GOBACK)
     currentPath = currentPath.Substring(0, currentPath.Length - Path.GetFileName(currentPath).Length - 1);
    else
     currentPath = Path.Combine(currentPath, currentFileName);
    UpdateControls();
   }
   else
   {
    //If it's a file, we should return the selected filename
    fileName = Path.Combine(currentPath, currentFileName);
    EndOk();
   }

The problem lies in the following line:

if ((currentItem.Tag as FileSystemKind?) != FileSystemKind.File)

I have tried two different online-converters which suggested me the following conversions (for above line):

1st one:

If (TryCast(currentItem.Tag, FileSystemKind)?) <> FileSystemKind.File Then

2nd one:

If TryCast(currentItem.Tag, System.Nullable(Of FileSystemKind)) <> FileSystemKind.File Then

The error I get in VB.Net is:

TryCast' operand must be reference type, but 'FileSystemKind?' is a value type.

The code is from a project targeting the Net.Compact Framework 2.0 but I think most should be compatible with the ordinary Compact Framework.

I am lost. Anyone who can help me out?

PS: I am sorry for the layout of the code in the question. Is there a way to change the font size to a smaller one?

Thanks!

+1  A: 
If TypeOf(currentItem.Tag) Is FileSystemKind AndAlso CType(currentItem.Tag, FileSystemKind) = FileSystemKind.File Then
ZippyV
He's not testing if it is an enum type, he's testing whether it is an enum value.
jfsk3
I've reread the question.
ZippyV
+1  A: 

If currentItem.Tag is always of type FileSystemKind you could try


If (DirectCast(currentItem.Tag, FileSystemKind) <> FileSystemKind.File) Then

if currentItem.Tag is not always of type FileSystemKind you could try


        If TypeOf (currentItem.Tag) Is FileSystemKind Then
            If (DirectCast(currentItem.Tag, FileSystemKind) <> FileSystemKind.File) Then
            End If
        Else
            ' handle different types
        End If

You may also use "CType" to convert or cast the type of the variant object "currentItem.Tag" to type FileSystemKind


If (CType(currentItem.Tag, FileSystemKind) <> FileSystemKind.File) Then
roygbiv: the statement you suggested "If (CType(currentItem.Tag, FileSystemKind) <> FileSystemKind.File) Then" resolved everything. Thanks.
moster67
+2  A: 

Load up your compiled .dll in Reflector, then change the view language to VB and it translates it for you.

 If (DirectCast(TryCast(currentItem.Tag,FileSystemKind?), FileSystemKind) <> FileSystemKind.File) Then
    End If
Mikael Svenson
Yeap- that would of been my suggestion but you beat to it.
RichardOD
Hej Mikael: yes, that's another converter I didn't think about. I'll keep that in mind next time I encounter similar issues.
moster67
It works for other .Net languages as well. And the output from it is shorter than the suggested solution further up. Vote me up :)
Mikael Svenson