#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Vb sub procedure


    hi, I have got some problem with the vb sub procedure

    Below is the program
    Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
    Dim a As String = "abc"
    cut(a)
    textbox1.Text =a
    End Sub
    Sub cut(ByRef a As String)
    a= a.Substring(2)
    End Sub

    for this the return is c

    but for another program
    Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
    Dim a As String = "abc"
    cut((a))
    textbox1.Text =a
    End Sub
    Sub cut(ByRef a As String)
    a= a.Substring(2)
    End Sub

    The return is abc

    Can any one explain it? Thanks
  2. #2
  3. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    OAKLAND CA | Adam's Point (Fairyland)
    Posts
    14,954
    Rep Power
    8617
    Both could be replaced by a single line of code. Those procedures look like they were built using the "smack it until it works" approach.

    Instead of passing things to a function you're using byref to operate on the variable in the sub. I don't know what platform you're on so a VBA equivalent would be:

    Code:
    Sub Cut(ByRef a As String)
        a = Mid(a, 2, 1)
    End Sub
    
    Sub Test()
        Dim c As String
        c = "abc"
        Cut c
        Debug.Print c
        c = "abc"
        Cut (c)
        Debug.Print c
    End Sub
    When you add the parenthesis () you're forcing it to pass by value instead of by reference. Write a function instead of this nonsense.
    Last edited by medialint; December 9th, 2011 at 01:34 PM.
    medialint.com

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Smile


    Originally Posted by medialint
    Both could be replaced by a single line of code. Those procedures look like they were built using the "smack it until it works" approach.

    Instead of passing things to a function you're using byref to operate on the variable in the sub. I don't know what platform you're on so a VBA equivalent would be:

    Code:
    Sub Cut(ByRef a As String)
        a = Mid(a, 2, 1)
    End Sub
    
    Sub Test()
        Dim c As String
        c = "abc"
        Cut c
        Debug.Print c
        c = "abc"
        Cut (c)
        Debug.Print c
    End Sub
    When you add the parenthesis () you're forcing it to pass by value instead of by reference. Write a function instead of this nonsense.
    Thanks for your help;

IMN logo majestic logo threadwatch logo seochat tools logo