There is a class in C # designed in the form of a library and connected to a project on VB.

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

Use in C # is

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

Trying to write on 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 but one line must be either with AddressOF or lambda expression.

 translate.ResultSelector = New Func(Of XElement, String) 

How this ResultSelector = x => x.Element("text").Value applied correctly. Brain blow...

    2 answers 2

    It is worth referring to help

    This line should look like this:

     translate.ResultSelector = Function(x) x.Element("text").Value 
    • the fact of the matter is that I reread it 10 times and still do not understand how it should be :) - Dmitry Gvozd
     Dim increment1 = Function(x) x + 1 

    See: https://msdn.microsoft.com/ru-ru/library/bb531253.aspx

    • And why is it poking in MSDN when there is already an accepted answer? - Dmitriy Gvozd