Google Translate API conversion - vb.net

Would anyone be able to help me with my translator using the google translate API? It comes up with an error on the very last line
System.InvalidCastException: 'Conversion from type 'TranslationResult' to type 'String' is not valid.'
This is the code here
Private Sub TranslateText(InputText)
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "C:\Users\student\Desktop\sunlit-runway-279702-175183ddeace.json")
Dim Client = TranslationClient.Create()
Dim InText = InputText.text
Dim response = Client.TranslateText(InText, LanguageCodes.Japanese, LanguageCodes.English)
Output.Text(response.TranslatedText)
End Sub
When I use a message box it works however I want it to be displayed in a textbox

Text is a property of textbox, not a function. To set the text of a textbox it's like this:
myTextBox.Text = "Hello word"
Not like this
myTextbox.Text("Hello world")
This is different to mesagebox.show which IS a function and DOES take a string parameter
MessageBox.Show("Hello World")
So, to set the textbox text to the translated result:
myTextbox.Text = response.TranslatedText 'translated text is a string, like "Hello World"
Really I think your code should look like:
Private Function TranslateJapanese(inputText as string) as string
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "C:\Users\student\Desktop\sunlit-runway-279702-175183ddeace.json")
Dim Client = TranslationClient.Create()
Dim response = Client.TranslateText(inputText, LanguageCodes.Japanese, LanguageCodes.English)
Return response.TranslatedText
End Sub
And be used like:
englishTextbox.Text = TranslateJapanese(japaneseTextbox.Text)

Related

Dynamic variable change VB.NET

I have this code in VB.NET:
Dim Hello As String = "123"
temp = Fix_this(hello)
Function Fix_this(The_content As String)
The_content = "456" : Return "test"
End Function
I want function Fix_This to change the string variable "Hello" without knowing the name of that variable even. I know I can return a new Hello string but I want to return something else.
Is there a way in Fix_this function, it can figure out the source of the "The_content" ?
So my goal is fix_this function to find out the source of The_content is "Hello" and then being able to dynamically change the Hello value in the function. No idea if I'm making any sense?
So let's say I succeed and I get string value "Hello". How do I then change the hello value? For example:
Source_of_The_content = "Hello"
If I do this:
Source_of_The_content = "New"
it obviously is not going to change the "Hello" string value to New.
If you want a Function or Sub to modify the parameter it receives, declare the parameter as ByRef. Using your Fix_thisfunction:
Function Fix_this(ByRef The_content As String) As String
The_content = "456"
Return "test"
End Function
If you run the following code, The_content will be "456":
Dim Hello As String = "123"
MsgBox("Hello before Fix_this: " & Hello)
Dim temp As String = Fix_this(Hello)
MsgBox("Hello after Fix_this: " & Hello)
MsgBox("temp: " & temp)
You can also do this by using a Sub if you are just interested in modifying The_content:
Sub Fix_this(ByRef The_content As String)
The_content = "456"
End Sub

Using a function to clean data in VBA

I am familiar with this post: How to Return a result from a VBA Function but changing my code does not seem to help.
I want to write a simple function in VBA that allows to lowercase an input sentence. I wrote this:
Private Function Converter(inputText As String) As String
Converter = LCase(inputText)
End Function
Sub test()
Dim new_output As String
new_output = Converter("Henk")
MsgBox (new_output)
End Sub
I tried following the advice I found at another stackoverflow post. I made me change this:
Private Function Converter(inputText As String)
Set outputText = LCase(inputText)
End Function
Sub test()
Dim new_output As String
Set new_output = Converter("Henk")
MsgBox (new_output)
End Sub
However, now I get an error that an object is required. Why does it require an object now? I dont get it...
Set outputText = LCase(inputText)
The Set keyword is reserved for Object data types. Unlike VB.NET, String in VBA is a basic data types.
So you dont Set a variable to a string. Drop the second version of your code altogether. It doesn't make sense. That "advice" was probably in another context.
To fix your first version
1- Assign the returned result to the name of the function Converter
2- It would be beneficial to specify explicitly the return type, as String. Currently it is a Variant that always embeds a String, so better make it explicit:
' vvvvvvvvv
Private Function Converter(inputText As String) As String
Converter = LCase(inputText) ' <------------ assign return to name of function
End Function

cast string to location

When I export a control's location to string, using the standard Control.Location.ToString method, I get the following:
{X=163,Y=196}
How can I turn this string back to location? Is there any out-of-the box function, or do I need to parse this string and do it myself?
Update:
I would like to serialize a few objects on the screen to XML to save progress and read it back later.
If you are serializing, then the serializer ought to handle the conversion from Point to string and back. If you examine the serializer output, it should look like this: 727, 97.
This form is InvariantString and NET provides the means to convert these without having to parse the text:
Dim pt = Button10.Location
Dim cvtr = TypeDescriptor.GetConverter(GetType(Point))
' convert Pt to invariant string
Dim strPT = cvtr.ConvertToInvariantString(pt)
Console.WriteLine(strPT)
' try to convert back
Dim pt2 = DirectCast(cvtr.ConvertFromInvariantString(strPT), Point)
If pt.Equals(pt2) Then
Console.Beep()
End If
Output:
727, 97
(Beep)
But again, a serializer should convert for you.
If you are doing lots of this, a generic version is handy:
Private Function CvtToInvariantString(Of T)(item As T) As String
Dim cvtr = TypeDescriptor.GetConverter(GetType(T))
Return cvtr.ConvertToInvariantString(item)
End Function
Private Function CvtFromInvariantString(Of T)(str As String) As T
Dim cvtr = TypeDescriptor.GetConverter(GetType(T))
Return DirectCast(cvtr.ConvertFromInvariantString(str), T)
End Function
Usage:
Dim strPT = CvtToInvariantString(pt)
Dim pt2 = CvtFromInvariantString(Of Point)(strPT)

Return a string from a VBA function

I am following a tutorial and get a compile error at the hello world example function.
What's wrong here?
Here is the code I tried:
Function hi()
hi = "hello world"
End Function`
edit: suggested declarations didn't help
edit: getting closer. brackets seem to be a problem when calling "hi()"
You can use 2 ways to implement your "Hello World" example.
Option 1: Simple and good enough for your example, using a regular Sub :
Sub Hi_()
Dim HiStr As String
HiStr = "Hello World"
MsgBox HiStr
End Sub
Option 2: Using a Function with "Hello World" example:
Function Hi(TestHi As String) As String
' Input: this function receives a string as a parameter
' Output: returns a string
Hi = "Test Function with " & TestHi
End Function
Now we need a Sub to test the Function:
Sub Test_Hi_Function()
Dim TstHiFunc As String
' send "Hello World" to Function Hi as a parameter
' TstHiFunc gets the returned string result
TstHiFunc = Hi("Hello World")
' for debug only
MsgBox TstHiFunc
End Sub

Error when assigning return value of ReadAllLines to variable

I am currently struggling with this function in Visual Studio:
Private Sub dlsuc()
Dim file_ As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\v.txt"
Dim file As String = IO.File.ReadAllLines(file_)
End Sub
I can't get this to work, I get something like these error messages:
Error 1 Value of type '1-dimensional array of String' cannot be converted to 'String'.
What is the reason for this?
This is because File.ReadAllLines() returns an array of strings. You can read more about it here.
Instead, try it like this:
Private Sub dlsuc()
Dim file_ As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\v.txt"
Dim file As String() = IO.File.ReadAllLines(file_)
End Sub
Notice the () in the variable declaration.