将c#app转换为VB

时间:2014-07-17 03:54:25

标签: c# vb.net code-translation

我正在将一个C#应用程序转换为VB,但我不确定是否会产生一些错误。

在C#中我有 -

 var quotesInvoker = new QuotesInvoker(new[] { "EUR=X", "JPY=X" });

哪个转换为 -

 Dim quotesInvoker = New Reuters_Downloader.HttpSample.Invokers.QuotesInvoker(New () {"EUR=X", "JPY=X"})

然而,New()出现'Type Expected'错误 如果我修改代码阅读 -

 Dim quotesInvoker = New Reuters_Downloader.HttpSample.Invokers.QuotesInvoker(CType(New Object() {"EUR=X", "JPY=X"}, String()))
问题消失但我不知道这是不是一个好的解决方案。有什么意见吗?

有了这个,我们有了这个C#QuotesInvoker代码 -

 public class QuotesInvoker : IInvoker<RetrieveItem_Request_3, ItemResponse_Short_Or_Full[]>
{
    private readonly string[] rics;

    public QuotesInvoker(string[] rics)
    {
        this.rics = rics;
    }

    public ItemResponse_Short_Or_Full[] Invoke(AuthorizationType authorization, RetrieveItem_Request_3 request)
    {
        var proxy = new Quotes_1Client();
        ItemResponse_Short_Or_Full[] response;
        proxy.RetrieveItem_3(null, authorization, request, out response);
        return response;
    }

    public RetrieveItem_Request_3 CreateRequest()
    {
        var retval = new RetrieveItem_Request_3();
        retval.TrimResponse = false;

        var request = new ItemRequest_2[1];
        var request0 = new ItemRequest_2();
        request0.Fields = "";
        var keys = new List<InstrumentKey_2>();
        foreach(var ric in rics)
        {
            keys.Add(new InstrumentKey_2
            {
                Name = ric,
                NameType = "RIC"
            });
        }
        request0.RequestKey = keys.ToArray();
        request0.ExpandChains = true;
        request0.Scope = Scope.All;
        request[0] = request0;

        retval.ItemRequest = request;
        return retval;
    }
}

转换为 -

    Public Class QuotesInvoker
    Implements IInvoker(Of RetrieveItem_Request_3, ItemResponse_Short_Or_Full())
    Private ReadOnly rics As String()

    Public Sub New(rics As String())
        Me.rics = rics
    End Sub

    Public Function Invoke(authorization As AuthorizationType, request As RetrieveItem_Request_3) As ItemResponse_Short_Or_Full()
        Dim proxy = New Quotes_1Client()
        Dim response As ItemResponse_Short_Or_Full() = Nothing
        proxy.RetrieveItem_3(Nothing, authorization, request, response)
        Return response
    End Function

    Public Function CreateRequest() As RetrieveItem_Request_3
        Dim retval = New RetrieveItem_Request_3()
        retval.TrimResponse = False

        Dim request = New ItemRequest_2(0) {}
        Dim request0 = New ItemRequest_2()
        request0.Fields = ""
        Dim keys = New List(Of InstrumentKey_2)()
        For Each ric In rics
            keys.Add(New InstrumentKey_2() With {  _
                Key .Name = ric, _
                Key .NameType = "RIC" _
            })
        Next
        request0.RequestKey = keys.ToArray()
        request0.ExpandChains = True
        request0.Scope = Scope.All
        request(0) = request0

        retval.ItemRequest = request
        Return retval
    End Function
End Class

这会在“Class'QuotesInvoker'第2行产生错误,必须实现'函数CreateRequest()As ThomsonReutersKnowledgeDirect.RetrieveItem_Request_3'用于接口'IInvoker(Of ThomsonReutersKnowledgeDirect.RetrieveItem_Request_3,ThomsonReutersKnowledgeDirect.ItemResponse_Short_Or_Full())'。”

“RetrieveItem_Request_3”的'无错误'Vb翻译是 -

<System.Diagnostics.DebuggerStepThroughAttribute()> _
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")> _
<System.ServiceModel.MessageContractAttribute(IsWrapped:=False)> _
Partial Public Class RetrieveItem_3_RequestMessage

    <System.ServiceModel.MessageHeaderAttribute([Namespace]:="http://www.reuters.com/ns/2008/03/01/webservices/rkd/Cache_1")> _
    Public CacheRequest As ThomsonReutersKnowledgeDirect.CacheRequest

    <System.ServiceModel.MessageHeaderAttribute([Namespace]:="http://www.reuters.com/ns/2006/05/01/webservices/rkd/Common_1")> _
    Public Authorization As ThomsonReutersKnowledgeDirect.AuthorizationType

    <System.ServiceModel.MessageBodyMemberAttribute([Namespace]:="http://www.reuters.com/ns/2006/05/01/webservices/rkd/Quotes_1", Order:=0)> _
    Public RetrieveItem_Request_3 As ThomsonReutersKnowledgeDirect.RetrieveItem_Request_3

    Public Sub New()
    End Sub

    Public Sub New(CacheRequest As ThomsonReutersKnowledgeDirect.CacheRequest, Authorization As ThomsonReutersKnowledgeDirect.AuthorizationType, RetrieveItem_Request_3 As ThomsonReutersKnowledgeDirect.RetrieveItem_Request_3)
        Me.CacheRequest = CacheRequest
        Me.Authorization = Authorization
        Me.RetrieveItem_Request_3 = RetrieveItem_Request_3
    End Sub
End Class

我迷失了这一点,所以将不胜感激任何帮助解决由此产生的翻译错误。 感谢

2 个答案:

答案 0 :(得分:1)

回答第一个问题:是的,就是这样。 VB编译器需要该语法的数组类型。你可以做一个修改:

Dim quotesInvoker = New Reuters_Downloader.HttpSample.Invokers.QuotesInvoker(New String() {"EUR=X", "JPY=X"})

至于你的第二个错误:C#编译器支持接口成员的隐式实现,而VB编译器则不支持。所以在VB中你应该总是明确指定要实现的成员:

Public Function CreateRequest() As RetrieveItem_Request_3 _
    Implements IInvoker(Of ThomsonReutersKnowledgeDirect.RetrieveItem_Request_3, ThomsonReutersKnowledgeDirect.ItemResponse_Short_Or_Full()).CreateRequest

答案 1 :(得分:1)

对于您的第一个问题,您可以使用:

Dim quotesInvoker = New QuotesInvoker( { "EUR=X", "JPY=X" })

对于第二个问题,只需添加必要的&#34; Implements&#34;子句(类型&#34;实现&#34;在方法头的末尾,VB将为您提供选择)。如果转换器没有接口代码或汇编引用来查找接口方法,则转换器不会为您添加这些。