paul1956 / CSharpToVB

New version of CSharpToVB converter
MIT License
26 stars 9 forks source link

Missing () #45

Closed VBAndCs closed 4 years ago

VBAndCs commented 4 years ago

When C# calls a property and adds () after it: viewStarts[i] = viewStartItem.PageFactory();

Check that PageFactory is a property and It returns a delegate. If so, add extra () in VB. C# doesn't allow params for properties, but VB does: viewStarts(i) = viewStartItem.PageFactory()()

paul1956 commented 4 years ago

@VBAndCs Need a more complete example, from the snippit I don't know PageFactory is a property.

paul1956 commented 4 years ago

@VBAndCs can you provide any open source file with this issue.

VBAndCs commented 4 years ago

You can create any property of type Func<T> and test it. Here is a sample:

using System;

namespace ConsoleAppCs
{
    class Program
    {

       static Func<int> Foo { get; } = () => 3;
        static void Main(string[] args)
        {
            var y = Foo();
            Console.WriteLine("Hello World!");
        }
    }
}

Resulted VB:

Option Explicit On
Option Strict On
Imports System

Namespace ConsoleAppCs
    Class Program

        Private Shared ReadOnly Property Foo As Func(Of Integer) = Function() 3
        Private Shared Sub Main(args As String())
            Dim y As Integer = Foo()
            Console.WriteLine("Hello World!")
        End Sub
    End Class
End Namespace

in VB code, Y should be a Func(of Integer) and you will get an error because you defined it as Integer. It is an Integer in C#, because it gets the result of calling the Func. So, you must add () in VB: Dim y As Integer = Foo()()

VBAndCs commented 4 years ago

As I said: C# properties don't have argument lists, so, when you see ([params]) after a property in C#, add empty () before it in VB.

paul1956 commented 4 years ago

@VBAndCs I have fixed this case using brute force, not sure I can always identify cases I need to do this with the information I have. Trial fix in 4.3.0.3 Converter 3.3.0.3.