I know that I can explicitly convert types in C# with the notation: (Type)Object
.
I am translating Visual Basic code to C#.
VB code:
TempTrans(j) = CType(FillTranslator.IndxLanguage.Item(j), Translator.IndxLangauges.IndxTranslation).Translations.Item(Row.Name.Trim) //This is the line I need help with!
Here is a struct (from the class Translator)
Structure IndxLangauges
Public IndxLanguage As Collection
Structure IndxTranslation
Public Language As Integer
Public Name As String
Public Translations As Collection
End Structure
End Structure
Also:
Private Shared FillTranslator As Translator.IndxLangauges
In C# I have:
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
I get the error: Cannot convert type string to Translator.IndxLanguages.IndxTranslation
I don't understand what is going on in the code directly following the conversion (In VB): .Translations.Item(Row.Name.Trim)
.
Could someone help me to understand the CType
code from VB, especially concerning the dot notation that follows?
You have translated this:
Public IndxLanguage As Collection
into
public System.Collections.Generic.List<string> IndxLanguage;
That's wrong, because IndxLanguage seems to hold elements of type IndxTranslation
, rather than elements of type String
.
You have two ways to fix that: Either translate the line literally using the exact same (legacy) type:
public Microsoft.VisualBasic.Collection IndxLanguage;
or (preferred) specify the correct item type:
public System.Collections.Generic.List<Translator.IndxLanguages.IndxTranslation> IndxLanguage;
That way, you won't even need the cast.
Note: A few using
s would greatly increase the readability of your code. For example, the previous line could be reduced to:
public List<IndxTranslation> IndxLanguage;
Note 2: Row.TypeName.Trim
is an invocation of String.Trim. In C#, method invocations require parenthesis, so it should read:
Row.TypeName.Trim()