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
Related
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
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)
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
Is it possible to return the arguments from the processPath in this example?
This might make more sense, sorry.
Dim processName As String
Dim processPath As String
If processName = "cmd" Then
Dim arguments As String() = Environment.GetCommandLineArgs()
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments))
End If
A simple (and clean) way to accomplish this would be to just modify your Sub Main as follows,
Sub Main(args As String())
' CMD Arguments are contained in the args variable
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", args))
End Sub
The VB.Net solution to your problem is to use Command() VB.Net function when you search to display command's line of currently executed process.
Sub Main(args As String())
Dim sCmdLine As String = Environment.CommandLine()
Console.WriteLine("CommandLine: " & sCmdLine)
Dim iPos = sCmdLine.IndexOf("""", 2)
Dim sCmdLineArgs = sCmdLine.Substring(iPos + 1).Trim()
Console.WriteLine("CommandLine.Arguments: " & sCmdLineArgs)
End Sub
The first outpout will display the complete command's line with name of program.
The second output will display only the command's line without program's name.
Using args is C/C++/C#/Java technic.
Using CommandLine() function is pure VB and is more intuitive because is return command's line as typed by user without supposing that arguments are type without blank.
Example:
LIST-LINE 1-12, WHERE=(20-24='TYPES'),to-area=4
LIST-LINE 1 - 12, WHERE = ( 20-24 = 'TYPES' ) , to-area = 4
In this command's syntax, arguments are separated by COMMA and not by spaces.
In this case, it is better to not use args technic that is more linked to C and Unix where command syntax accepts arguments separated by space !
Another option
Sub WhatEver()
Dim strArg() as string
strArg = Command().Split(" ")
' strArg(0) is first argument and so on
'
'
End Sub
i can't found on any website (yeah.. i took a good couple or hours searching/Reading/testing), a way to call a simple Hello World Function by Return, and show it to the user.
Here is my WCF class code:
Imports System.ServiceModel
Imports System.ServiceModel.Activation
<ServiceContract(Namespace:="Servicio")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class ServicioBD
<OperationContract()>
Public Function ReturnString(ByVal number As Integer) As String
If number = 1 Then
Return "Hello World!"
Else
Return "Bye World!"
End If
End Function
I added this service with the name "ServicioWCF_BD".
And this is how im calling it from the MainPage.xaml:
Private Sub SayHello()
Dim wcf As New ServicioWCF_BD.ServicioBDClient
Dim message As String = wcf.ReturnStringAsync(1) 'ERROR THE EXPRESSION DOESNT GENERATE A VALUE
MessageBox.Show(message)
End Sub
When i try to get the value from the function visual says the error: "The expression doesnt generate a value".
I hope you can help me, actually its the first time im stuck so hard with something that looks so simple.....
* sigh *
The return value of wcf.ReturnStringAsync(1) is, most probably, of type Task and not string.
Your SayHello sub should look something like:
Private Sub SayHello()
Dim wcf As New ServicioWCF_BD.ServicioBDClient
Dim message As String = wcf.ReturnStringAsync(1).Result
MessageBox.Show(message)
End Sub
Or, depending where you're calling it from:
Private Async Function SayHello() As Task
Dim wcf As New ServicioWCF_BD.ServicioBDClient
Dim message As String = Await wcf.ReturnStringAsync(1)
MessageBox.Show(message)
End Sub