Click to See Complete Forum and Search --> : Functions?


wilton
January 7th, 2000, 06:43 PM
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?

January 7th, 2000, 08:55 PM
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

wilton
January 7th, 2000, 09:09 PM
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.

January 8th, 2000, 07:12 AM
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