How to write a lymbda for this case
-
Class C# is available as a library and is connected to the VB project.
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Web; using System.Xml.Linq;
namespace YandexTranslate
{public enum RequestType { Translate = 0, Detect = 1 } public struct TranslateCommand { public RequestType Type; public Dictionary<string, string> Parameters; public Func<XElement, string> ResultSelector; } public class TextTranslationAPI { private readonly string Url; private readonly string Key; public TextTranslationAPI(string url, string key) { Url = url; Key = key; } public TextTranslationAPI() : this(@"https://translate.yandex.net/api/v1.5/tr/", "trnsl.1.1.XXXXXX") { } private string GetAction(RequestType type) { switch (type) { case RequestType.Detect: return "detect"; case RequestType.Translate: return "translate"; default: throw new ArgumentOutOfRangeException(); } } public string GetResult(TranslateCommand command) { string strUrl = Url + GetAction(command.Type) + "?key=" + Key; foreach (var parameter in command.Parameters) strUrl += "&" + parameter.Key + "=" + HttpUtility.UrlEncode(parameter.Value); WebClient webClitnt = new WebClient(); webClitnt.Encoding = Encoding.UTF8; string stringXml = webClitnt.DownloadString(strUrl); XDocument document = XDocument.Parse(stringXml); string textTranslate = command.ResultSelector(document.Root); return textTranslate; } }
}
C# such use
TranslateCommand translate = new TranslateCommand{
Type = RequestType.Translate,
Parameters = new Dictionary<string,string>{ {"lang", translateLanguage}, {"text", text} },
ResultSelector = x => x.Element("text").Value
};
string result = api.GetResult(translate);
I'm trying to write VB.
Dim translate As TranslateCommand = New TranslateCommand()
translate.Type = RequestType.Translate translate.Parameters = New Dictionary(Of String, String) translate.Parameters.Add("lang", "EN") translate.ResultSelector = New Func(Of XElement, String) Dim result As String = api.GetResult(translate)
The last line should be either with the addressOF or the lymbda expression.
translate.ResultSelector = New Func(Of XElement, String)
Like this.
ResultSelector = x => x.Element("text").Value
Apply correctly. Brain withdrawal... ♪
-
It's worth asking. https://msdn.microsoft.com/ru-ru/library/bb531253.aspx
This line should look like:
translate.ResultSelector = Function(x) x.Element("text").Value