2

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?

Servy
  • 202,030
  • 26
  • 332
  • 449
5unnyr4y3
  • 85
  • 1
  • 2
  • 11
  • See also *[Difference between DirectCast() and CType() in VB.NET](https://stackoverflow.com/questions/3056514)* (2010). – Peter Mortensen Jun 09 '21 at 14:47
  • Possible duplicate (though it doesn't seem to be canonical (10 votes and from 2012)): *[How do I translate VB.NET's CType() to C#?](https://stackoverflow.com/questions/12249528/how-do-i-translate-vb-nets-ctype-to-c)* – Peter Mortensen Jun 09 '21 at 14:48
  • There must be a canonical question somewhere (the general question in the title that ***search engines*** include in their result). This must have been asked in the first few days or weeks of Stack Overflow's existence. Can someone find it? There must be one from 2008 or 2009. This is from 2010, but only eight votes: *[What is the C# equivalent of CType in VB.NET?](https://stackoverflow.com/questions/4409082)* – Peter Mortensen Jun 09 '21 at 14:52
  • More canonical (50 votes and from 2010): *[C#'s equivalent to VB.NET's DirectCast?](https://stackoverflow.com/questions/2683847)* – Peter Mortensen Jun 09 '21 at 14:58

1 Answers1

4

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 usings 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()
Heinzi
  • 167,459
  • 57
  • 363
  • 519