Results 1 to 4 of 4

Thread: Translate this from C# to vb

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    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

    Parksie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. AddHandler report.Error, Sub(innerSender As Object, eventArgs As ErrorEventArgs)
    2.                              Me.ReportViewer.Report = Nothing
    3.                              Me.Label1.Text = eventArgs.Exception.Message.ToString()
    4.                          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.
    Last edited by jmcilhinney; Aug 9th, 2012 at 05:13 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Translate this from C# to vb

    Thanks jmcilhinney.
    I shall give that a go and keep you updated.

    Parksie

  4. #4

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Translate this from C# to vb

    Thanks.
    You are a God among men

    Parksie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width