wergeld
wergeld

Reputation: 14462

Converting API Example From C# To VB For DoL API

I am trying to convert this sample code from the US Department of Labor API documentation from C# to VB (very cool if I can get it to work. Check it out here). I am not making the MVC example. When I use any online converters I get error:

-- line 1 col 11: invalid TypeDecl 

This is the C# code:

protected void Page_Load(object sender, EventArgs e)
    {
        AgencyEntities entity = new AgencyEntities(new Uri(“http://api.dol.gov/V1/DOLAgency”));
        entity.SendingRequest += new EventHandler<SendingRequestEventArgs>(DOLDataUtil.service_SendingRequest);
        AgenciesView.DataSource = entity.Agencies;
        AgenciesView.DataBind();
    }

How can I get this wired up in VB? When I did a line by line change using the VB Page_Load I get this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim entity As New AgencyEntities(New Uri("http://api.dol.gov/V1/DOLAgency"))

    entity.SendingRequest += New EventHandler(Of SendingRequestEventArgs)(DOLDataUtil.service_SendingRequest)
    AgenciesView.DataSource = entity.Agencies
    AgenciesView.DataBind()

End Sub

But, the line

entity.SendingRequest += New EventHandler(Of SendingRequestEventArgs)(DOLDataUtil.service_SendingRequest)

fails with several errors:

  • Error 1 'Public Event SendingRequest(sender As Object, e As System.Data.Services.Client.SendingRequestEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
  • Error 2 Delegate 'System.EventHandler(Of System.Data.Services.Client.SendingRequestEventArgs)' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.

What am I missing here?

Upvotes: 0

Views: 816

Answers (1)

David W
David W

Reputation: 10184

Replace the offending line with:

AddHandler entity.SendingRequest, AddressOf DOLDataUtil.service_SendingRequest

And see if that helps...VB doesn't support that "+" syntax for adding event handlers.

Upvotes: 2

Related Questions