.: Help in making a Function..
Can somebody show me how to make a good Function using a certain Declaration, let's say
Code:
Declare Function Beep Lib "kernel32.dll" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
I just need a simple example that would be easily used but useful. Something that would make me easier to call or use this type of declaration.
Thanks so much. :)
Re: .: Help in making a Function..
Can you explain a little more?
Do you want to make your own VB function? Or do you want to make a function, put it in a DLL file, and call it like you would an API function? (like the Beep() one you posted).
Re: .: Help in making a Function..
Yes. I want to make one for my own function using the declaration. But if you have another simpler type of declaration, please show me a how to.
Thanks.
Re: .: Help in making a Function..
VB functions don't need to be done like that...example:
vb Code:
Private Function MyAdd(ByVal Number1 As Long, ByVal Number2 As Long) As Long
MyAdd = Number1 + Number2
End Function
And then calling the function...
vb Code:
Private Sub Form_Load()
MsgBox MyAdd(3, 2) '5
End Sub
Is that what you mean?
Re: .: Help in making a Function..
Yep, exactly! But can you show the example of using Beep Declaration and make a Function out of it?
Because there's a line called 'ByVal' that makes me confused.
There are also dwFreq (i know this is a frequency), but is it only a matter of renaming it into dwFreq, but not 'somethingFreq'?
Thanks a lot. :)
Re: .: Help in making a Function..
To use the Beep API function, you just put:
vb Code:
Private Sub Command1_Click()
Beep 1000, 50
End Sub
That plays a tone of 1,000MHz (or 1KHz) for 50 milliseconds.
If you have a question about an API function, look here:
http://allapi.mentalis.org/apilist/apilist.php
Each one usually has one or more code examples along with it.
And about ByVal...
ByVal means an argument is passed to the function "By Value". As oppose to ByRef (By Reference).
ByVal makes a copy of that variable. ByRef passes a pointer to where that variable is in memory. For this reason, arguments passed ByRef can be modified by that function. Here's an example of where ByRef is useful:
vb Code:
Private Sub TestByRef(ByRef Argument1 As String, ByRef Argument2 As Integer)
Argument1 = "something"
Argument2 = 1234
End Sub
Private Sub Form_Load()
Dim strWillChange As String, intWillChange As Integer
strWillChange = "This will change when calling the sub"
intWillChange = 1
TestByRef strWillChange, intWillChange
MsgBox strWillChange, vbInformation
MsgBox intWillChange, vbInformation
End Sub
As you can see, the sub was able to modify those variables (strWillChange, intWillChange) directly. If it was passed ByVal, it can't directly do that.
If there is no "ByVal" or "ByRef" there, it will pass arguments ByRef by default.
Hopefully you're not completely confused now. :D That stuff isn't really important for your Beep question anyway...
Re: .: Help in making a Function..
Forgot to say, if you want to make your own "wrapper" for the Beep function, (which is kind of pointless), then you can do:
vb Code:
Private Function MyBeep(ByVal Frequency As Long, ByVal Duration As Long) As Long
MyBeep = Beep(Frequency, Duration)
End Function
Re: .: Help in making a Function..
Wow, thank you very much :D I appreciate your kindness. though I haven't really get the point of how to differentiate ByRef and ByVal.
Thanks a lot.