Results 1 to 8 of 8

Thread: .: Help in making a Function..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    265

    Question .: 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.

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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).

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    265

    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.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: .: Help in making a Function..

    VB functions don't need to be done like that...example:

    vb Code:
    1. Private Function MyAdd(ByVal Number1 As Long, ByVal Number2 As Long) As Long
    2.     MyAdd = Number1 + Number2
    3. End Function

    And then calling the function...

    vb Code:
    1. Private Sub Form_Load()
    2.     MsgBox MyAdd(3, 2) '5
    3. End Sub

    Is that what you mean?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    265

    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.

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: .: Help in making a Function..

    To use the Beep API function, you just put:

    vb Code:
    1. Private Sub Command1_Click()
    2.     Beep 1000, 50
    3. 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:
    1. Private Sub TestByRef(ByRef Argument1 As String, ByRef Argument2 As Integer)
    2.     Argument1 = "something"
    3.     Argument2 = 1234
    4. End Sub
    5.  
    6. Private Sub Form_Load()
    7.     Dim strWillChange As String, intWillChange As Integer
    8.     strWillChange = "This will change when calling the sub"
    9.     intWillChange = 1
    10.  
    11.     TestByRef strWillChange, intWillChange
    12.  
    13.     MsgBox strWillChange, vbInformation
    14.     MsgBox intWillChange, vbInformation
    15. 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...

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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:
    1. Private Function MyBeep(ByVal Frequency As Long, ByVal Duration As Long) As Long
    2.     MyBeep = Beep(Frequency, Duration)
    3. End Function

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    265

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width