1

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!

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

Yeap- that would of been my suggestion but you beat to it.
Hej Mikael: yes, that's another converter I didn't think about. I'll keep that in mind next time I encounter similar issues.
It works for other .Net languages as well. And the output from it is shorter than the suggested solution further up. Vote me up :)
2

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

1 Comment

roygbiv: the statement you suggested "If (CType(currentItem.Tag, FileSystemKind) <> FileSystemKind.File) Then" resolved everything. Thanks.
1
If TypeOf(currentItem.Tag) Is FileSystemKind AndAlso CType(currentItem.Tag, FileSystemKind) = FileSystemKind.File Then

1 Comment

He's not testing if it is an enum type, he's testing whether it is an enum value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.