Skip to content

Commit c1c5218

Browse files
Deal with extern sub - probably fixes #168
1 parent ccc9d51 commit c1c5218

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

ICSharpCode.CodeConverter/CSharp/NodesVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ public override CSharpSyntaxNode VisitDeclareStatement(VBSyntax.DeclareStatement
762762
var attributeLists = ConvertAttributes(node.AttributeLists).Add(dllImportAttributeList);
763763

764764
var modifiers = CommonConversions.ConvertModifiers(node.Modifiers).Add(SyntaxFactory.Token(SyntaxKind.StaticKeyword)).Add(SyntaxFactory.Token(SyntaxKind.ExternKeyword));
765-
var returnType = (TypeSyntax)node.AsClause?.Type.Accept(TriviaConvertingVisitor);
765+
var returnType = (TypeSyntax)node.AsClause?.Type.Accept(TriviaConvertingVisitor) ?? SyntaxFactory.ParseTypeName("void");
766766
var parameterListSyntax = (ParameterListSyntax)node.ParameterList?.Accept(TriviaConvertingVisitor) ??
767767
SyntaxFactory.ParameterList();
768768

Tests/CSharp/StatementTests.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,16 @@ private void TestMethod()
550550
}");
551551
}
552552

553-
[Fact]
554-
public void DeclareStatement()
553+
[Theory]
554+
[InlineData("Sub", "", "void")]
555+
[InlineData("Function", " As Long", "long")]
556+
public void DeclareStatement(string vbMethodDecl,string vbType, string csType)
555557
{
556558
// Intentionally uses a type name with a different casing as the loop variable, i.e. "process" to test name resolution
557-
TestConversionVisualBasicToCSharp(@"Imports System.Diagnostics
559+
TestConversionVisualBasicToCSharp($@"Imports System.Diagnostics
558560
559561
Public Class AcmeClass
560-
Private Declare Function SetForegroundWindow Lib ""user32"" (ByVal hwnd As Int32) As Long
562+
Private Declare {vbMethodDecl} SetForegroundWindow Lib ""user32"" (ByVal hwnd As Int32){vbType}
561563
562564
Public Shared Sub Main()
563565
For Each process In Process.GetProcesses().Where(Function(p) Not String.IsNullOrEmpty(p.MainWindowTitle))
@@ -566,24 +568,24 @@ For Each process In Process.GetProcesses().Where(Function(p) Not String.IsNullOr
566568
Next
567569
End Sub
568570
End Class"
569-
, @"using System;
571+
, $@"using System;
570572
using System.Diagnostics;
571573
using System.Linq;
572574
573575
public class AcmeClass
574-
{
576+
{{
575577
[System.Runtime.InteropServices.DllImport(""user32"")]
576-
private static extern long SetForegroundWindow(Int32 hwnd);
578+
private static extern {csType} SetForegroundWindow(Int32 hwnd);
577579
578580
public static void Main()
579-
{
581+
{{
580582
foreach (var process in Process.GetProcesses().Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)))
581-
{
583+
{{
582584
SetForegroundWindow(process.MainWindowHandle.ToInt32());
583585
Thread.Sleep(1000);
584-
}
585-
}
586-
}");
586+
}}
587+
}}
588+
}}");
587589
}
588590

589591
[Fact]

0 commit comments

Comments
 (0)