Reputation: 101
I want to create a google translator link, that depends on my input. To make things clearer, here my "dynamic link":
private static string _GoogleLink = $"https://translate.google.com/?hl=de#{_SourceLanguage}/{_TargetLanguage}/{_SourceInput}"
I think it´s clear what i try to do, but the link doesn´t work this way, it just enters
https://translate.google.com/?hl=de#//
in my browser. I execute the link with this method:
public static void Translate(string input, string sourceLang, string targetLang)
{
_SourceInput = input;
_TargetLanguage = targetLang;
_SourceLanguage = sourceLang;
System.Diagnostics.Process.Start(_GoogleLink);
}
Upvotes: 0
Views: 55
Reputation: 7440
Your static field gets evaluated upon the static creation, it never gets "updated" with the correct values - use a property instead of an field, which gets evaluated every time the getter is called.
private static string _GoogleLink => $"https://translate.google.com/?hl=de#{_SourceLanguage}/{_TargetLanguage}/{_SourceInput}";
Or old School Syntax
private static string _GoogleLink { get { return $"https://translate.google.com/?hl=de#{_SourceLanguage}/{_TargetLanguage}/{_SourceInput}"; } }
But you should consider re-designing your method to this:
//instead of void - use string as a return
public static string Translate(string input, string sourceLang, string targetLang)
{
//instead of saving your passed values, into static fields - transform the string right here and now and return it
return $"https://translate.google.com/?hl=de#{sourceLang}/{targetLang}/{input}";
}
I believe this syntax is more understandable.
Upvotes: 1