2014-02-25 35 views
2

此代碼有什麼問題: 使用newton.jsoft(json.net)將c#翻譯爲vb.net時出錯。在Visual Studio 2012: 原始代碼:在Visual Studio 2012中使用newton.jsoft(json.net)將c#翻譯爲vb .net的翻譯錯誤

public static List<TradeInfo> GetTrades(BtcePair pair) 
    { 
     string queryStr = string.Format("https://btc-e.com/api/2/{0}/trades", BtcePairHelper.ToString(pair)); 
     return JArray.Parse(WebApi.Query(queryStr)).OfType<JObject>().Select(TradeInfo.ReadFromJObject).ToList(); 
    } 

翻譯代碼:

Public Shared Function GetTrades(pair As BtcePair) As List(Of TradeInfo) 
    Dim queryStr As String = String.Format("https://btc-e.com/api/2/{0}/trades", _ 
               BtcePairHelper.ToString(pair)) 
    Return JArray.Parse(WebApi.Query(queryStr)).OfType(Of JObject)().[Select](TradeInfo.ReadFromJObject).ToList() 
End Function 

錯誤:

Error 1 Argument not specified for parameter 'o' of 'Public Shared Function ReadFromJObject(o As Newtonsoft.Json.Linq.JObject) As TradeInfo'.

調用的函數(VB):

Public Shared Function ReadFromJObject(o As JObject) As TradeInfo 
    If o Is Nothing Then 
     Return Nothing 
    End If 

Return New TradeInfo() With 
    {.Amount = o.Value(Of Decimal)("amount"), _ 
    .Price = o.Value(Of Decimal)("price"), _ 
    .[Date] = UnixTime.ConvertToDateTime(o.Value(Of Long)("date")), _ 
    .Item = BtceCurrencyHelper.FromString(o.Value(Of String)("item")), _ 
    .PriceCurrency = BtceCurrencyHelper.FromString(o.Value(Of String) 
    ("price_currency")), _ 
    .Tid = o.Value(Of Long)("tid"), _ 
    .Type = TradeInfoTypeHelper.FromString(o.Value(Of String)("trade_type")) _ 
    } 
End Function 
+1

IIRC,你需要'AddressOf'。 – SLaks

+1

你的C#和VB版本根本不相似 - 你是以什麼方式將一個轉換爲另一個? –

+0

使用在線工具在http://www.developerfusion.com/tools/convert/csharp-to-vb/ –

回答

2

我想你只需要一個 「AddressOf」 因爲你不是實際調用 'ReadFromJObject':

Public Shared Function GetTrades(ByVal pairlist() As BtcePair, Optional ByVal limit As Integer = 150) As Dictionary(Of BtcePair, List(Of TradeInfoV3)) 
    Dim tradeInfoListReader As Func(Of JContainer, List(Of TradeInfoV3)) = (Function(x) x.OfType(Of JObject)().Select(AddressOf TradeInfoV3.ReadFromJObject).ToList()) 
    Return MakeRequest(Of List(Of TradeInfoV3))("trades", pairlist, tradeInfoListReader, New Dictionary(Of String, String)() From {{ "limit", limit.ToString() }}, True) 
End Function 
+0

謝謝,這似乎是工作。 –

1

首先,而VB代碼返回一個List C#代碼返回一個字典。其次,這兩種方法有不同的參數數量和類型。我沒有超越那個。