locked
Passing values to a function and returning value

    Question

  • Still learning passing values.  Could someone check my code so far and see if I am heading in the right direction??  I am getting value not declared errors and I see it, but not quite sure on where to declare them.

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim A, B, D As Integer

    A = 1

    B = 2

    D = 5

    AddThem()

    MsgBox("Sum is " & c)

    End Sub

    Public Function AddThem()

    Dim C As Integer

    C = A + B + D

    End Function

    Monday, June 19, 2006 6:43 PM

Answers

  • rewrite your code as to make all variables public and declare them outside the scope of any function so that they are globally accesible to all functions.

     

     

     

    Dim A, B, C , D As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    A = 1

    B = 2

    D = 5

    AddThem()

    MsgBox("Sum is " & c)

    End Sub

    Public Function AddThem()

    C = A + B + D

    End Function

     

     

     

    for more information refer to this link

    http://www.dummies.com/WileyCDA/DummiesArticle/id-1936.html

    Monday, June 19, 2006 6:56 PM
  • Another solution:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim AnArray(2) As Integer

    AnArray(0)  = 1

    AnArray(1)  = 2

    AnArray(2) = 5

    MsgBox("Sum is " & cstr(AddThem(AnArray)))

    End Sub

    Public Function AddThem(byVal ArrayOfIntegers() as integer)as integer

    Dim TheAnswer as integer

    for x as integer = 0 to ubound(ArrayOfIntegers)

    TheAnswer = TheAnswer + ArrayOfIntegers(x)

    Next

    return TheAnswer

    End Function

     

    Monday, June 19, 2006 7:30 PM

All replies

  • rewrite your code as to make all variables public and declare them outside the scope of any function so that they are globally accesible to all functions.

     

     

     

    Dim A, B, C , D As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    A = 1

    B = 2

    D = 5

    AddThem()

    MsgBox("Sum is " & c)

    End Sub

    Public Function AddThem()

    C = A + B + D

    End Function

     

     

     

    for more information refer to this link

    http://www.dummies.com/WileyCDA/DummiesArticle/id-1936.html

    Monday, June 19, 2006 6:56 PM
  • Another solution:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim AnArray(2) As Integer

    AnArray(0)  = 1

    AnArray(1)  = 2

    AnArray(2) = 5

    MsgBox("Sum is " & cstr(AddThem(AnArray)))

    End Sub

    Public Function AddThem(byVal ArrayOfIntegers() as integer)as integer

    Dim TheAnswer as integer

    for x as integer = 0 to ubound(ArrayOfIntegers)

    TheAnswer = TheAnswer + ArrayOfIntegers(x)

    Next

    return TheAnswer

    End Function

     

    Monday, June 19, 2006 7:30 PM
  • The function must pass a value back to the function name, the editor should be telling you this with squigly blue lines, if the routine only had to add them then it should be a sub, as far as I can see the rest of it looks fine.

    A better idea would be to do this:

    Public Function AddThem() as Integer

    AddThem= A+B+D

    End Function

    when you want a result of the values, simply put:

    Msgbox (AddThem)

    You might have to put AddThem.ToString as the msgbox arg if your Vb is in a funny mood and wants a string. lol.

    Monday, June 19, 2006 10:20 PM