Results 1 to 14 of 14

Thread: overloading a function in VB6?

  1. #1

    Thread Starter
    Lively Member Scorpion965's Avatar
    Join Date
    Jan 2002
    Location
    Laguna Hills, CA
    Posts
    100

    overloading a function in VB6?

    Hello...

    I have a function that creates file paths for my program, essentially you would call the function in the following manner:

    Dim MyPath as string

    MyPath = MyFilePath("myexe.exe")

    and MyPath would now be something along the lines of "C:\MyDir\myProgram\myexe.exe"

    I also have places where I need just the directory, ie "C:\MyDir\myProgram\", this is where I would like to overload the MyFilePath function to return the file path when it is sent a string and to return the directory when it isn't sent any parameters. This is a simple exercise in other languages but I cannot seem to find an example code snippet for this in VB6, if anyone knows how to overload functions in VB I would appreciate it...

    Thanks

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Can you post the code for your function? We might be able to modify it for the same purpose.

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586
    Couldn't you just do this in your MyFilePath fuction..

    VB Code:
    1. If trim(strFName) = "" then
    2.      MyFilePath = BaseDirectory
    3. End If
    4. 'This is assuming your varialbe is named strFName and your variable holding your directory is BaseDirectory.

    Like the above poster said if you post your code we can probably modify it to work.. but I think the above code entered in would work too. Then you just call it as MyPath = MyFilePath("") to get the directory.

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    You could also make it Optional, ex:

    VB Code:
    1. Private Function MyFilePath(Optional FilePath As String = "") As String
    2. If FilePath = "" Then
    3.     MsgBox "You didn't pass anything."
    4. Else
    5.     MsgBox "You passed " & FilePath
    6. End If
    7. End Function

    I only say this for your future reference, as it does the exact same thing as what's been posted above (except you can pass nothing, ie. MyFilePath() instead of MyFilePath(""), but that's negligable in this case).

    Basically, Optional makes the FilePath argument (guess what) optional, meaning if you don't pass it, it doesn't error as it would normally. Now, the '= ""' makes it equal to "" if there is nothing passed for FilePath. Hope this helps in the future
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  5. #5
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Or you could use ByRef parameters to return multiple results and have the function return an error code. Windows API work this way.

    The example below is unoptimized and more error checking must be done. But I hope it leads you to a viable and correct option to approach your problem.

    VB Code:
    1. Private Function MyPathAndFileAndExtensionAndSize(ByRef strFName, ByRef strPName, ByRef strXName, ByRef lngSize) As Long
    2. 'Generate an error as the default
    3. MyPathAndFileAndExtensionAndSize = -1
    4.  
    5. 'As this can always be retrieved, get it
    6. strPName = App.Path & "\"
    7.  
    8. 'If no filename argument is passed then
    9. 'only the path is retrieved
    10. 'and an error code of 2 is set
    11. '
    12. 'If a valid filename is passed then
    13. 'the file name is formatted with the path
    14. 'the extension is found
    15. 'and the file size is retrieved
    16. If Len(strFName) > 0 Then
    17.     strFName = App.Path & "\" & strFName
    18.     strXName = Right$(strFName, Len(strFName) - InStr(1, strFName, ".", vbTextCompare))
    19.     lngSize = FileLen(strFName)
    20.     MyPathAndFileAndExtensionAndSize = 1
    21. Else
    22.     strFName = "No file name passed"
    23.     strXName = "No file name passed"
    24.     lngSize = -1
    25.     MyPathAndFileAndExtensionAndSize = 2
    26. End If
    27.  
    28. End Function
    29.  
    30.  
    31. Private Sub Command1_Click()
    32. Dim strFile As String
    33. Dim strPath As String
    34. Dim strExt As String
    35. Dim lngSize As Long
    36.  
    37. Dim strResult As String
    38.  
    39. strFile = "Form1.frm"
    40. 'strFile = ""
    41.  
    42. If MyPathAndFileAndExtensionAndSize(strFile, strPath, strExt, lngSize) <> -1 Then
    43.     strResult = "The fully qualified file name is " & strFile & vbCrLf & vbCrLf
    44.     strResult = strResult & "The path is " & strPath & vbCrLf & vbCrLf
    45.     strResult = strResult & "The extension of the file is " & strExt & vbCrLf & vbCrLf
    46.     strResult = strResult & "The size of the file is " & lngSize & " bytes"
    47. Else
    48.     strResult = "An Error occured"
    49. End If
    50.    
    51. MsgBox strResult
    52. End Sub

    HTH

    Regards

    Kaushik Janardhanan

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  6. #6
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    VB is not an OO language and as such does not support overloading, you'll just have to work around it, using the examples given here or din some other way

  7. #7
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Perhaps the only time a Variant is a *must* in VB6!

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by KayJay
    Perhaps the only time a Variant is a *must* in VB6!
    True, although this feature would have been extremely handy for some of the stuff we do. I'm assuming VB.NET has overloading? Someone correct me if I'm wrong.

  9. #9
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    VB.NET if fully OO and has overloading, proper inheritance and polymorphism.

  10. #10
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  11. #11

  12. #12
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    A better example than any thing I could whip up!

    http://www.vb-faq.com/Articles/Zimme...omingVB7_3.asp

    Check out the hyperlink in the para begining with "Overloading permits more elegance.."

    Also take a look at this discussion

    VB6 does not support function overloading. (You can try looking into
    ParamArray and the Variant data type; these might allow you to simulate
    function overloading.)

    Long and short of it, only a Variannt can accomodate multiple data types for parameters (unless one takes into consideration a ParamArray) which FAPP and perhaps even theoratically a Variant.

    HTH

    Regards

    Kaushik Janardhanan

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by MartinLiss
    What do you mean?
    To work with different datatypes. Declare a function with the argument as a variant, check it's datatype, and perform different tasks based upon what type it is. So you can OBJECTNAME.add(something1, something2) with strings, integers, etc.

    That's one example.

  14. #14
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

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