Results 1 to 8 of 8

Thread: [RESOLVED] Functions

Hybrid View

  1. #1

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Resolved [RESOLVED] Functions

    Can someone explain to me how to make a custom function.
    Like a function that takes and returns something.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Functions

    Like this:
    VB Code:
    1. 'Function FunctionName(arguments) as ReturnType
    2. Function FunctionName(Arg1 As Long, Arg2 As String) As String
    3. '
    4. ''Function body
    5. '
    6.     FunctionName = ReturnData      ' <-- returns ReturnData to caller
    7. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Lively Member
    Join Date
    Mar 2005
    Posts
    103

    Re: Functions

    it's really easy!

    Public Function function_name(var1 as integer, var2 as string) as integer


    in the function you can have some calculations. at the end u just write

    function_name = solution


    End Function


    to call a function you use the function like this

    variable = function_name(var1, "abc")

    or

    if function_name(var1,"abc") = true then

  4. #4
    Hyperactive Member vincentg's Avatar
    Join Date
    Jun 2005
    Location
    Chicago IL, USA
    Posts
    261

    Re: Functions

    You can do it within the Form (General) or you create a module
    If you have a MSDN try to look for
    Function Statement Visual Basic for Applications Reference

    Details are well there.

  5. #5

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Re: Functions

    Ok, I tried one. What is wrong with this it never stops looping when I use it.
    Code:
    Function LoopSend(txt As String, i As Integer)
    Dim i2 As Integer
    i2 = 0
    Do While (i2 < i)
    SendKeys txt
    i2 = i2 + 1
    Loop
    End Function

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Functions

    Based on what you posted, it is difficult to say.

    What did you pass it for parameters, i.e., what does i equal? What does txt equal?

    Also, functions return a value but your function is not set up that way. Basically, what you have is a Sub. To be a true function, you would declare is as something like[Highlight=VB]Private Function LoopSend(txt As String, i As Integer) As ? - what do you want returned? A string? An integer? A long?

  7. #7
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Functions

    Why don't you use a For loop? That's what it's for. And be sure to use [VBCODE] tags, not [CODE] tags for VB code

    Also indenting your code makes it easier to read and debug. VB.NET automatically
    indents it for you, thank God.

    VB Code:
    1. Function LoopSend(txt As String, ByVal i As Long) As Boolean
    2.  
    3.     Dim i2 As Long 'Always best to work with Longs cause its the fastest
    4.                     'computing data type and there's no worry for overflow.
    5.  
    6.     For i2 = 0 To i
    7.  
    8.         SendKeys txt
    9.      
    10.     Next i
    11.  
    12.     LoopSend = True
    13.  
    14. End Function

  8. #8

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Re: Functions

    Ok thanks works.

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