【问题标题】:Converting API Example From C# To VB For DoL API为 DoL API 将 API 示例从 C# 转换为 VB
【发布时间】:2012-08-30 18:16:53
【问题描述】:

我正在尝试将美国劳工部 API 文档中的示例代码从 C# 转换为 VB(如果我可以让它工作,那就太棒了。检查一下 here)。我制作 MVC 示例。当我使用任何在线转换器时出现错误:

-- line 1 col 11: invalid TypeDecl 

这是 C# 代码:

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();
    }

如何在 VB 中实现这一点? 当我使用 VB Page_Load 逐行更改时,我得到了这个:

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

但是,这条线

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

失败并出现几个错误:

  • 错误 1 ​​'公共事件 SendingRequest(sender As Object, e As System.Data.Services.Client.SendingRequestEventArgs)' 是一个事件,并且 不能直接调用。使用“RaiseEvent”语句来引发 事件。
  • 错误 2 委托 'System.EventHandler(Of System.Data.Services.Client.SendingRequestEventArgs)' 需要一个 'AddressOf' 表达式或 lambda 表达式作为唯一参数 它的构造函数。

我在这里错过了什么?

【问题讨论】:

标签: c# .net vb.net c#-4.0


【解决方案1】:

将有问题的行替换为:

AddHandler entity.SendingRequest, AddressOf DOLDataUtil.service_SendingRequest

看看这是否有帮助...VB 不支持添加事件处理程序的“+”语法。

【讨论】:

  • 在 (DOLDataUtil.service_SendingRequest) 上给出错误,即“未为‘公共共享子服务_SendingRequest(作为对象发送,e 作为 System.Data.Services.Client.SendingRequestEventArgs) 的参数‘e’指定参数” .也没有指定“发件人”。
  • 我从 DOLDataUtil.service_SendingRequest 周围删除了“()”,它现在可以工作了。
  • 如果您编辑答案以删除 () 我会将其标记为答案。
  • 感谢编辑,@Meta-Knight :) 被叫走,无法修复!
猜你喜欢
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
相关资源
最近更新 更多