Results 1 to 5 of 5

Thread: Parameters

  1. #1

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Parameters

    Ok this is becoming a joke annoying. I'm trying to get my head around parameters. I can write them and they work (sort of) but when i try and experiment it goes down hill.

    This is my code (the one that works)

    VB Code:
    1. 'Needed
    2. '1 Command Button
    3. '2 Text Boxes
    4. Private Sub Parameter(ByVal ValueOne As String, _
    5.                       ByVal ValueTwo As String)
    6.    
    7.     ValueOne = "Hello"
    8.     ValueTwo = "World"
    9.    
    10.     Text1.Text = ValueOne
    11.     Text2.Text = ValueTwo
    12.    
    13. End Sub
    14.  
    15. Private Sub Command1_Click()
    16.     Call Parameter(Text1.Text, Text2.Text)
    17. End Sub

    Now, let me explain how i think this works. in the Parameter sub we have two variables that are passed By Value, in this sub we also assign the values to the variables, we then allocate the variables to objects (text boxes).

    Then, from the button click event we call the paramter sub and the arguments that we are carrying over are text1.text and text2.text. So are these arguments the recievers as in that is where the values are going or are we copying them from the procedure. if we are copying them from the procedure then why can i not use

    VB Code:
    1. Private Sub Command1_Click()
    2.     Call Parameter(ValueOne, ValueTwo)
    3. End Sub

    Also if text1 and text2 are the receivers for the values(where the values will end up) then why can i not use variables to hold the values then allocate those values to objects.

    can anyone explain to me how parameters work because it is simple things like this that i can not solve and makes me think that i am not cut out ot be a programmer

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Re: Parameters

    In this sub you would not normally do the highlighted lines.

    VB Code:
    1. Private Sub Parameter(ByVal ValueOne As String, _
    2.                       ByVal ValueTwo As String)
    3.    
    4.     [HL="#FFFF80"]ValueOne = "Hello"
    5.     ValueTwo = "World"[/HL]
    6.    
    7.     Text1.Text = ValueOne
    8.     Text2.Text = ValueTwo
    9.    
    10. End Sub

    The reason for that is that if you did Call Paremeter("Hi", There") (and by the way that can be done as follows Paremeter "Hi", There") the "Hi" and the "There" would immediately be replaced by your "Hello" and "World".

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Re: Parameters

    Here is a simpler example.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim x As String
    5.     x = "original"
    6.     Sub1 x
    7.     MsgBox x '<== "original"
    8.     Sub2 x
    9.     MsgBox x '<== "changed"
    10. End Sub
    11. Public Sub Sub1(ByVal x As String)
    12.     x = "This won't change the originating x"
    13. End Sub
    14. Public Sub Sub2(ByRef x As String)
    15.     x = "changed" 'This will change the originating x
    16. End Sub

  4. #4

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Parameters

    So how would it normally be done? Do you have an example?

    Jenova

  5. #5

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