[英].Net - Problems converting code-snippet from C# to VB.Net
將以下代碼段從C#轉換為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();
}
問題在於以下幾行:
if ((currentItem.Tag as FileSystemKind?) != FileSystemKind.File)
我嘗試了兩個不同的在線轉換器,這些轉換器建議我進行以下轉換(針對上述行):
第一個:
If (TryCast(currentItem.Tag, FileSystemKind)?) <> FileSystemKind.File Then
第二個:
If TryCast(currentItem.Tag, System.Nullable(Of FileSystemKind)) <> FileSystemKind.File Then
我在VB.Net中得到的錯誤是:
“ TryCast”操作數必須是引用類型,但“ FileSystemKind?” 是值類型。
該代碼來自針對Net.Compact Framework 2.0的項目,但我認為大多數代碼應與普通的Compact Framework兼容。
我搞不清楚了。 有人可以幫助我嗎?
PS:很抱歉問題代碼的布局。 有沒有辦法將字體大小更改為較小的字體?
謝謝!
在Reflector中加載已編譯的.dll,然后將視圖語言更改為VB並為您翻譯。
If (DirectCast(TryCast(currentItem.Tag,FileSystemKind?), FileSystemKind) <> FileSystemKind.File) Then
End If
如果currentItem.Tag始終為FileSystemKind類型,則可以嘗試
If (DirectCast(currentItem.Tag, FileSystemKind) <> FileSystemKind.File) Then
如果currentItem.Tag 並非始終為FileSystemKind類型,則可以嘗試
If TypeOf (currentItem.Tag) Is FileSystemKind Then
If (DirectCast(currentItem.Tag, FileSystemKind) <> FileSystemKind.File) Then
End If
Else
' handle different types
End If
您也可以使用“ CType”將變量對象“ currentItem.Tag”的類型轉換或轉換為FileSystemKind類型。
If (CType(currentItem.Tag, FileSystemKind) <> FileSystemKind.File) Then
If TypeOf(currentItem.Tag) Is FileSystemKind AndAlso CType(currentItem.Tag, FileSystemKind) = FileSystemKind.File Then
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.