[RESOLVED] Change string value from a function?
Is it possible to change the value of a string from a function, because I'm having a problem here?
The general idea would be like below, but the string will not change, so is there any way I make that happen?
vb.net Code:
Public Class Form1
Dim MyItem1, MyItem2, MyItem3 As Integer
Public Function SetItemStats(ByVal CurrentItemSlot As ComboBox, ByVal CurrentItemName As String)
CurrentItemName = CurrentItemSlot.Text
End Function
Private Sub SetItem1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetItem1Button.Click
SetItemStats(item1list, MyItem1) ' item1list is the name of my combobox.
MsgBox(MyItem1) ' Comes out empty here
End Sub
End Class
EDIT: Fixed that by changing ByVal into ByRef, I'm such an idiot.
Re: Change string value from a function?
there are 2 problems there:
Code:
Public Function SetItemStats(ByVal CurrentItemSlot As ComboBox, ByVal CurrentItemName As String)
CurrentItemName = CurrentItemSlot.Text
'no return???
End Function
try this instead:
Code:
Public Sub SetItemStats(ByVal CurrentItemSlot As ComboBox, ByRef CurrentItemName As String)
CurrentItemName = CurrentItemSlot.Text
End Sub
Re: Change string value from a function?
Quote:
Originally Posted by
.paul.
there are 2 problems there:
Code:
Public Function SetItemStats(ByVal CurrentItemSlot As ComboBox, ByVal CurrentItemName As String)
CurrentItemName = CurrentItemSlot.Text
'no return???
End Function
try this instead:
Code:
Public Sub SetItemStats(ByVal CurrentItemSlot As ComboBox, ByRef CurrentItemName As String)
CurrentItemName = CurrentItemSlot.Text
End Sub
Yeah, changing it into ByRef fixes it. Anyway, is there a problem if I leave it as a function? I know since it doesn't return anything it should be a Sub, but I prefer using the word function instead of sub, or just adding a Return 0 maybe :D
Thanks btw.
Re: Change string value from a function?
you can leave it as a function + return an unused value if you add a return type
Re: Change string value from a function?
Yeah, doing that. Thanks again and marking as solved now.