CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Functions?

  1. #1

    Functions?

    I want to write my own function, and I want to be able to pass down variables that will be modified. Just like in C++. How do I do that. I don't want a bunch of public or global variables either. Is there a way?


  2. #2
    Guest

    Re: Functions?



    private Sub Command1_Click()
    Dim strFirst as string
    Dim intFirst as Integer
    strFirst = Text1
    intFirst = Text2
    Call Test(strFirst, intFirst)
    MsgBox strFirst & " and " & intFirst
    End Sub

    private Function Test(byref MyString as string, byref MyNumber as Integer)
    MyString = "These are modified values: " & MyString & " " & MyString
    MyNumber = 2 * MyNumber
    Test = MyString & " " & MyNumber
    End Function



    Place Command1, 2 text boxes on the form and paste code. Run and enter in the first box some string, in the second digit and click Command1. ByRef is included just to clarify the code. You can omit it, because it's default.
    Vlad


  3. #3

    Re: Functions?

    Does this mean that in this code:

    Sub CallTimer(Flag as string)

    'Starts and stops the timer
    Select Case Flag
    Case "Create":
    If blnTimer = false then
    lngTimerID = SetTimer(0, 0, (lnGTimerCnt * 1000), AddressOf TimerProc)
    If lngTimerID = 0 then
    MsgBox "Timer not created. Ending Program."
    Exit Sub
    End If
    'Tell program: timer was created
    MsgBox "Timer Created"
    blnTimer = true

    ElseIf blnTimer = true then
    'Attempt to kill the timer
    'lngTimerID = KillTimer(0, lngTimerID)
    'If Timer wasn't killed
    'If lngTimerID = 0 then
    MsgBox "error: Timer already exists.", vbCritical, "error"
    'blnTimer = true
    'else timer was killed
    End If
    Case "Reset":
    If blnTimer = true then
    'Attempt to kill the timer



    The passed values are being referenced like pointers in C++ or that the values are merely being passed? I'm not quite sure.


  4. #4
    Guest

    Re: Functions?

    I know C not well enough to be shure, but I think so. ByRef modifier (it's default in VB) means that you pass address of value (I think it's the same as pointer or very similar), not value itself. ByVal modifier passes value itself. Actually it is not absolutly true (it adds null terminating character to string variable), but to simplify the issue, you can think that it is true.
    Vlad


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

Featured