Translate this from C# to vb
Sorry about the double post guys but not too many of the VB guys know c#. Then again maybe you guys know little about VB :)
I have been given this example code to help solve a problem I'm working on but I need to convert it to VB which I know little about.
The various code convertors out there get it wrong.
Any ideas how VB handles this :
Code:
report.Error += delegate(object innerSender, ErrorEventArgs eventArgs)
{
this.ReportViewer.Report = null;
this.Label1.Text = eventArgs.Exception.Message.ToString();
};
Thanks In Advance
Re: Translate this from C# to vb
It doesn't. That's an anonymous method which is a C#-only feature. That said, anonymous methods have basically been superseded by lambda expressions in C# and they are supported in VB too. VB 2008 only supports value lambdas, i.e. functions, and even then only a single line of code is allowed, so you can only do that in VB 2010. In VB, you use the AddHandler statement to attach a handler to an event. You need to specify the delegate to attach, which you usually do by specifying the AddressOf the method that will handle the event. In VB 2010 you can also use an action lambda, i.e. a subroutine.
vb.net Code:
AddHandler report.Error, Sub(innerSender As Object, eventArgs As ErrorEventArgs)
Me.ReportViewer.Report = Nothing
Me.Label1.Text = eventArgs.Exception.Message.ToString()
End Sub
You can in VB 2010 as well but in VB 2008 you would have no choice but to actually declare a regular named method and then use AddressOf to specify that as the event handler.
By the way, that use of ToString is pointless because the Message property of an exception is type String to begin with.
Re: Translate this from C# to vb
Thanks jmcilhinney.
I shall give that a go and keep you updated.
Re: Translate this from C# to vb
Thanks.
You are a God among men