|
-
Oct 22nd, 2003, 10:20 PM
#1
Thread Starter
Lively Member
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
-
Oct 22nd, 2003, 10:34 PM
#2
Can you post the code for your function? We might be able to modify it for the same purpose.
-
Oct 22nd, 2003, 11:06 PM
#3
Fanatic Member
Couldn't you just do this in your MyFilePath fuction..
VB Code:
If trim(strFName) = "" then
MyFilePath = BaseDirectory
End If
'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.
-
Oct 22nd, 2003, 11:16 PM
#4
You could also make it Optional, ex:
VB Code:
Private Function MyFilePath(Optional FilePath As String = "") As String
If FilePath = "" Then
MsgBox "You didn't pass anything."
Else
MsgBox "You passed " & FilePath
End If
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.
-
Oct 23rd, 2003, 02:46 AM
#5
Frenzied Member
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:
Private Function MyPathAndFileAndExtensionAndSize(ByRef strFName, ByRef strPName, ByRef strXName, ByRef lngSize) As Long
'Generate an error as the default
MyPathAndFileAndExtensionAndSize = -1
'As this can always be retrieved, get it
strPName = App.Path & "\"
'If no filename argument is passed then
'only the path is retrieved
'and an error code of 2 is set
'
'If a valid filename is passed then
'the file name is formatted with the path
'the extension is found
'and the file size is retrieved
If Len(strFName) > 0 Then
strFName = App.Path & "\" & strFName
strXName = Right$(strFName, Len(strFName) - InStr(1, strFName, ".", vbTextCompare))
lngSize = FileLen(strFName)
MyPathAndFileAndExtensionAndSize = 1
Else
strFName = "No file name passed"
strXName = "No file name passed"
lngSize = -1
MyPathAndFileAndExtensionAndSize = 2
End If
End Function
Private Sub Command1_Click()
Dim strFile As String
Dim strPath As String
Dim strExt As String
Dim lngSize As Long
Dim strResult As String
strFile = "Form1.frm"
'strFile = ""
If MyPathAndFileAndExtensionAndSize(strFile, strPath, strExt, lngSize) <> -1 Then
strResult = "The fully qualified file name is " & strFile & vbCrLf & vbCrLf
strResult = strResult & "The path is " & strPath & vbCrLf & vbCrLf
strResult = strResult & "The extension of the file is " & strExt & vbCrLf & vbCrLf
strResult = strResult & "The size of the file is " & lngSize & " bytes"
Else
strResult = "An Error occured"
End If
MsgBox strResult
End Sub
HTH
Regards
Kaushik Janardhanan
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Oct 23rd, 2003, 07:24 AM
#6
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
-
Oct 23rd, 2003, 07:51 AM
#7
Frenzied Member
Perhaps the only time a Variant is a *must* in VB6!
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Oct 23rd, 2003, 07:56 AM
#8
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.
-
Oct 23rd, 2003, 07:59 AM
#9
VB.NET if fully OO and has overloading, proper inheritance and polymorphism.
-
Oct 23rd, 2003, 08:03 AM
#10
Frenzied Member
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Oct 23rd, 2003, 08:06 AM
#11
Originally posted by KayJay
Perhaps the only time a Variant is a *must* in VB6!
What do you mean?
-
Oct 23rd, 2003, 08:16 AM
#12
Frenzied Member
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
-
Oct 23rd, 2003, 08:16 AM
#13
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.
-
Oct 23rd, 2003, 08:19 AM
#14
Frenzied Member
"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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|