C# 等效于具有 CType 的 Visual Basic 代码
C# equivalent of Visual Basic Code with CType
我知道我可以使用以下符号在 C# 中显式转换类型:(Type)Object
。
我正在将 Visual Basic 代码转换为 C#。
VB 代码:
TempTrans(j) = CType(FillTranslator.IndxLanguage.Item(j), Translator.IndxLangauges.IndxTranslation).Translations.Item(Row.Name.Trim) //This is the line I need help with!
这是一个结构体(来自class 翻译器)
Structure IndxLangauges
Public IndxLanguage As Collection
Structure IndxTranslation
Public Language As Integer
Public Name As String
Public Translations As Collection
End Structure
End Structure
还有:
Private Shared FillTranslator As Translator.IndxLangauges
在 C# 中我有:
public struct IndxLanguages
{
public System.Collections.Generic.List<string> IndxLanguage;
public struct IndxTranslation
{
public int Language;
public string Name;
public System.Collections.Generic.List<string> Translations;
};
};
private static Translator.IndxLanguages FillTranslator;
TempTrans[j] = ((Translator.IndxLanguages.IndxTranslation)FillTranslator.IndxLanguage[j]).Translations[Row.TypeName.Trim]; //Error here
我收到错误:无法将类型字符串转换为 Translator.IndxLanguages.IndxTranslation
我不明白转换后代码中发生了什么(在 VB 中):.Translations.Item(Row.Name.Trim)
。
有人可以帮助我理解 VB 中的 CType
代码,尤其是后面的点符号吗?
您已经翻译了这个:
Public IndxLanguage As Collection
进入
public System.Collections.Generic.List<string> IndxLanguage;
这是错误的,因为 IndxLanguage 似乎包含 IndxTranslation
类型的元素,而不是 String
.
类型的元素
您有两种解决方法:使用完全相同的 (legacy) type:
字面翻译 行
public Microsoft.VisualBasic.Collection IndxLanguage;
或(首选)指定正确的项目类型:
public System.Collections.Generic.List<Translator.IndxLanguages.IndxTranslation> IndxLanguage;
那样的话,你甚至不需要演员表。
注意:一些 using
s 会大大增加代码的可读性。例如,上一行可以简化为:
public List<IndxTranslation> IndxLanguage;
注2:Row.TypeName.Trim
是对String.Trim的调用。在 C# 中,方法调用需要括号,因此应为:
Row.TypeName.Trim()
我知道我可以使用以下符号在 C# 中显式转换类型:(Type)Object
。
我正在将 Visual Basic 代码转换为 C#。
VB 代码:
TempTrans(j) = CType(FillTranslator.IndxLanguage.Item(j), Translator.IndxLangauges.IndxTranslation).Translations.Item(Row.Name.Trim) //This is the line I need help with!
这是一个结构体(来自class 翻译器)
Structure IndxLangauges
Public IndxLanguage As Collection
Structure IndxTranslation
Public Language As Integer
Public Name As String
Public Translations As Collection
End Structure
End Structure
还有:
Private Shared FillTranslator As Translator.IndxLangauges
在 C# 中我有:
public struct IndxLanguages
{
public System.Collections.Generic.List<string> IndxLanguage;
public struct IndxTranslation
{
public int Language;
public string Name;
public System.Collections.Generic.List<string> Translations;
};
};
private static Translator.IndxLanguages FillTranslator;
TempTrans[j] = ((Translator.IndxLanguages.IndxTranslation)FillTranslator.IndxLanguage[j]).Translations[Row.TypeName.Trim]; //Error here
我收到错误:无法将类型字符串转换为 Translator.IndxLanguages.IndxTranslation
我不明白转换后代码中发生了什么(在 VB 中):.Translations.Item(Row.Name.Trim)
。
有人可以帮助我理解 VB 中的 CType
代码,尤其是后面的点符号吗?
您已经翻译了这个:
Public IndxLanguage As Collection
进入
public System.Collections.Generic.List<string> IndxLanguage;
这是错误的,因为 IndxLanguage 似乎包含 IndxTranslation
类型的元素,而不是 String
.
您有两种解决方法:使用完全相同的 (legacy) type:
字面翻译 行public Microsoft.VisualBasic.Collection IndxLanguage;
或(首选)指定正确的项目类型:
public System.Collections.Generic.List<Translator.IndxLanguages.IndxTranslation> IndxLanguage;
那样的话,你甚至不需要演员表。
注意:一些 using
s 会大大增加代码的可读性。例如,上一行可以简化为:
public List<IndxTranslation> IndxLanguage;
注2:Row.TypeName.Trim
是对String.Trim的调用。在 C# 中,方法调用需要括号,因此应为:
Row.TypeName.Trim()