Jump to content

Passing string from one private sub to another private sub

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
terrylau

terrylau

    Newbie

  • Members
  • Pip
  • 6 posts
Hi, need suggestion or help to pass string from one private sub to another private sub. For example :

Private Sub 1
stringB = stringA

Private Sub 2
stringA

How do I pass the stringA to stringB?
Have been reading around but not so sure about some of the comments on using ByVal, byRef or even using Public Sub and Module.

Appreciate any input into this. Thanks in advance.

#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Byval

 

Private sub Example1()

    Example2("Hello","CodeCall",5)

End Sub

 

Private sub Example2(byval str1 as string, byval str2 as string, byval n as integer)

   for i as integer  1 to n

      messagebox.show(str1 & " " & str2)

   next

End Sub


Example1 is here calling Example2, str1's value will be "Hello", str2's value will be "CodeCall" and n's value will be 5.

Then Example2 will use these to show 5 messageboxes with the text "Hello CodeCall".


Byref


Private Sub Example1()

   Dim FirstValue as integer

   Dim SecondValue as integer

   Dim sum as integer


   FirstValue = 5

   SecondValue = 7


   Example2(FirstValue,SecondValue,sum)

  

   Messagebox.show(sum)



End Sub



Private Sub Example2(byval a as integer, byval b as integer, [B]byref[/B] c as integer)

   c=a+b

End Sub


Since the variable c is byref, the sum variable which is used as the c when calling Example2 the variable sum will be changed when the c variable changes.

This code will therefor show a messagebox with the message "12"

If variable c were byval, variable c would have got the value of a+b even though the variable sum wouldn't change in value.


Hope you understood :D