Reputation: 173
I am really struggling to get my head around passing parameters to Functions.
I want to use Functions so that I don't have to keep repeating blocks of code which I understand is what they are used for. The function will return something back to what called it.
Example:
I have wrote this function which gets the value of Name from the Registry and returns it to the variable s
Public Function DecodingModes(s As String)
s = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Ian\MyApp\", "Name", Nothing)
Return s
End Function
In a Button Click Event I have the following.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MyName As String
MyName = CType(DecodingModes("what goes in here"), String)
MsgBox(MyName)
End Sub
My interperation of what it does is this...
What I don't understand is
MyName = CType(DecodingModes("what goes in here"), String)
Why does it not matter what I put in the (" ")
I have watched tutorials online about Functions and passing parameters but they all seem to be dealing with Numbers and not text.
Can anyone help
Ian
Upvotes: 0
Views: 100
Reputation: 46
Here's a very basic example:
Private Function Add(No1 As Integer, No2 As Integer) As Integer
Return No1 + No2
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox3.Text = Add(CInt(TextBox1.Text), CInt(TextBox2.Text))
End Sub
So when Button1 is clicked the contents of Textbox1 and Textbox2 are added together and the result is put in Textbox3
As a piece of code this is useless as it's a very long winded way of adding 2 numbers together, but to show what is going on it's quite helpful to keep it this simple.
Private Function Add(No1 As Integer, No2 As Integer) As Integer
So I've named the function 'Add' and then stated that it's going to be passed two integers (No1 and No2), and that I want an integer as the return value (the final 'As Integer' outside the brackets)
TextBox3.Text = Add(CInt(TextBox1.Text), CInt(TextBox2.Text))
And here I'm saying that I want Textbox3 to contain the return value of passing the values of Textbox1 and Textbox2 to the Add function.
If you were already working with integers it would be a little cleaner looking:
Dim Int1, Int2 as integer
Int1 = 15
Int2 = 84
With these two integers configured, the code to add them would look like this:
TextBox3.Text = Add(Int1, Int2)
I hope I've explained clearly and concisely enough, but I'm sure someone will correct me if I've said something stupid.
Upvotes: 0
Reputation: 2685
For a correct programming approach you must replace your entire Function
with:
Public Function DecodingModes()
Return My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Ian\MyApp\", "Name", Nothing)
End Function
There is not “s” needed as a parameter as you are going to recreate this by every call of DecodingModes
function
A Function generally is made to elaborate something then return you as result. If you have a variable that needs to be elaborated, or, other elaboration (inside your function) needs them as a part of elaboration, you can pass it as parameter. Another thing you need to remember as a correct programming technique is that you should specify the return type (eg. String, Integer or other) otherwise your function return type is always an Object type, that means you need to convert type every call.
Upvotes: 2
Reputation: 18
Because you're passing the text "what goes in here" to your DecodingModes function, then within your DecodingModes you completely disregard the argument that you passed in ("what goes in here") for "s", and you pull the value for "s" from the registry query that you do. Therefore no matter what value you pass to your function, the result will be constant since you don't actually use the parameter "s".
As g3nt-m3caj said, the parameter "s" that you pass in is redundant and doesn't need to be there since it's not used.
Upvotes: 0