Results 1 to 5 of 5

Thread: [RESOLVED] Change string value from a function?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Resolved [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:
    1. Public Class Form1
    2.  
    3. Dim MyItem1, MyItem2, MyItem3 As Integer
    4.  
    5. Public Function SetItemStats(ByVal CurrentItemSlot As ComboBox, ByVal CurrentItemName As String)
    6.         CurrentItemName = CurrentItemSlot.Text
    7. End Function
    8.  
    9. Private Sub SetItem1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetItem1Button.Click
    10.         SetItemStats(item1list, MyItem1) ' item1list is the name of my combobox.
    11.         MsgBox(MyItem1) ' Comes out empty here
    12. End Sub
    13.  
    14. End Class

    EDIT: Fixed that by changing ByVal into ByRef, I'm such an idiot.
    Last edited by Legjendat; Jan 11th, 2013 at 01:34 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Re: Change string value from a function?

    Quote Originally Posted by .paul. View Post
    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

    Thanks btw.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Change string value from a function?

    you can leave it as a function + return an unused value if you add a return type

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Re: Change string value from a function?

    Yeah, doing that. Thanks again and marking as solved now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width