|
-
Mar 22nd, 2008, 05:06 AM
#1
Thread Starter
Hyperactive Member
.: 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.
Last edited by vibiEn; Mar 22nd, 2008 at 05:36 AM.
Reason: Really need a help.
-
Mar 22nd, 2008, 06:14 AM
#2
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).
-
Mar 22nd, 2008, 06:20 AM
#3
Thread Starter
Hyperactive Member
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.
-
Mar 22nd, 2008, 07:00 AM
#4
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?
-
Mar 22nd, 2008, 07:05 AM
#5
Thread Starter
Hyperactive Member
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.
Last edited by vibiEn; Mar 22nd, 2008 at 07:53 AM.
-
Mar 22nd, 2008, 08:07 AM
#6
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. That stuff isn't really important for your Beep question anyway...
-
Mar 22nd, 2008, 08:09 AM
#7
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
-
Mar 22nd, 2008, 08:24 AM
#8
Thread Starter
Hyperactive Member
Re: .: Help in making a Function..
Wow, thank you very much I appreciate your kindness. though I haven't really get the point of how to differentiate ByRef and ByVal.
Thanks a lot.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|