Richtig konvertieren / C# -> vb.net / CSharpImpl obsolete

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Richtig konvertieren / C# -> vb.net / CSharpImpl obsolete

    Hi!

    Ich versuch gerade ein C# Projekt nach vb.net zu konvertieren.
    Leider wandelt der Visual Studio Converter nicht alles korrekt um.

    Er erstellt eine bzw. zwei Funktionen die CSharpImpl __Assign bzw. __Throw benutzen..(Obsolete)
    ..ich hätte dies gern ausgemerzt.

    Ist dieser Code korrekt?:

    Quelle C#:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. public CommentFrame([NotNull] string comment, [NotNull] string description)
    2. {
    3. Comment = comment ?? throw new ArgumentNullException(nameof(comment));
    4. Description = description ?? throw new ArgumentNullException(nameof(description));
    5. }
    6. public override bool Equals(Id3Frame other)
    7. {
    8. return other is CommentFrame comment &&
    9. comment.Language == Language &&
    10. comment.Description == Description;
    11. }


    wurde zu:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Sub New(<NotNull> comment As String, <NotNull> description As String)
    2. Me.Comment = If(comment, CSharpImpl.__Throw(Of String)(New ArgumentNullException(NameOf(comment))))
    3. Me.Description = If(description, CSharpImpl.__Throw(Of String)(New ArgumentNullException(NameOf(description))))
    4. End Sub
    5. Public Overrides Function Equals(ByVal other As Id3Frame) As Boolean
    6. Dim comment As CommentFrame = Nothing
    7. Return CSharpImpl.__Assign(comment, TryCast(other, CommentFrame)) IsNot Nothing AndAlso comment.Language = Language AndAlso Equals(comment.Description, Description)
    8. End Function
    9. Private Class CSharpImpl
    10. <Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
    11. Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
    12. target = value
    13. Return value
    14. End Function <Obsolete("Please refactor calling code to use normal throw statements")>
    15. Shared Function __Throw(Of T)(ByVal e As Exception) As T
    16. Throw e
    17. End Function
    18. End Class


    Ich hätte das jetzt so übersetzt:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Sub New(<NotNull> comment As String, <NotNull> description As String)
    2. Me.Comment = If(comment, New ArgumentNullException(NameOf(comment)))
    3. Me.Description = If(description, New ArgumentNullException(NameOf(description)))
    4. End Sub
    5. Public Overrides Function Equals(ByVal other As Id3Frame) As Boolean
    6. Dim comment As CommentFrame = TryCast(other, CommentFrame)
    7. Return comment IsNot Nothing AndAlso comment.Language = Language AndAlso comment.Description = Description
    8. End Function


    Ist das so korrekt?
    Sieht falsch aus, den einen string als boolean auszuwertem kann denke ich nicht gehen.

    Hab dir das eben von Hand gemacht, so ist es richtig:

    VB.NET-Quellcode

    1. Public Sub New(comment As String, description As String)
    2. If String.IsNullOrEmpty(comment) Then
    3. Throw New ArgumentNullException("comment")
    4. End If
    5. If String.IsNullOrEmpty(description) Then
    6. Throw New ArgumentNullException("description")
    7. End If
    8. Me.Description = description
    9. Me.Comment = comment
    10. End Sub

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Takafusa“ ()