From 3175901033cf0e638de0bbe6782e9d9a06de55cc Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 15 Jan 2006 14:53:43 +0000 Subject: [PATCH] Fixed NullReferenceException when converting C# statements like "if (a != null) b.c();" git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@991 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Visitors/CSharpConstructsVisitor.cs | 5 +++- .../Output/VBNet/CSharpToVBConverterTest.cs | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpConstructsVisitor.cs b/src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpConstructsVisitor.cs index de6b57b911..a94a4c5ae8 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpConstructsVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpConstructsVisitor.cs @@ -101,7 +101,10 @@ namespace ICSharpCode.NRefactory.Parser } if (ident != null && se != null) { InvocationExpression ie = se.Expression as InvocationExpression; - if (ie != null && (ie.TargetObject as IdentifierExpression).Identifier == ident.Identifier) { + if (ie != null && + ie.TargetObject is IdentifierExpression && + (ie.TargetObject as IdentifierExpression).Identifier == ident.Identifier) + { ReplaceCurrentNode(new RaiseEventStatement(ident.Identifier, ie.Arguments)); } } diff --git a/src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs b/src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs index c8da50e3b7..9fdad66b5a 100644 --- a/src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs +++ b/src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs @@ -124,6 +124,29 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter "RaiseEvent MyEvent(Me, EventArgs.Empty)"); } + [Test] + public void IfStatementSimilarToRaiseEvent() + { + TestStatement("if (FullImage != null) DrawImage();", + "If FullImage IsNot Nothing Then\n" + + "\tDrawImage()\n" + + "End If"); + // regression test: + TestStatement("if (FullImage != null) e.DrawImage();", + "If FullImage IsNot Nothing Then\n" + + "\te.DrawImage()\n" + + "End If"); + // with braces: + TestStatement("if (FullImage != null) { DrawImage(); }", + "If FullImage IsNot Nothing Then\n" + + "\tDrawImage()\n" + + "End If"); + TestStatement("if (FullImage != null) { e.DrawImage(); }", + "If FullImage IsNot Nothing Then\n" + + "\te.DrawImage()\n" + + "End If"); + } + [Test] public void AnonymousMethod() {