Is the following possible? I want the user to perform a method that returns a Folderitem. If something goes wrong I don’t want to return that Folderitem but some error code. There are different error codes. I know I can only define one return type but is there a workaround?
if everythingOK then
Define the return value as Variant.
Better, raise an exception. Variants should be avoided when possible.
Another alternative: add a ByRef integer parameter to the function. Before returning, set that integer to the error code, or to a “no error” code if it’s good.
Lispcomb is right. It’s bad practice to mix the meanings of your returns, it’ll cause many problems depending on the usage of your function.
If a function is complex enough to fail, I’ll do what Lispcomb suggested, return the requested “thing” as a ByRef argument and reserve the return value for status. That’s the common practice - Apple and Microsoft do that in all their API’s. But if a function is guaranteed not to fail - usually for simplicity sake - pass the desired “thing” as the return value.
I don’t raise exceptions usually but I don’t think you have to. Though I do do it within the function itself and pass on a error code in the return value when it hits the eception handler area.
I agree with the byref method, but suggest you swap the variables
FUNCTION myFunction(byref f as folderitem) as boolean
..... some code
if everythingOK then
If myFunction(f)=TRUE THEN
msgbox "Sorry I failed"
You don’t need to pass the folderitem ByRef.
You could also have the return data type to be a FolderItem and in case of an error you just return Nil. Your code then asks the other instance or module, what the last error was.
Dim fi As FolderItem = obj.TheMethod()
If fi Is Nil Then
// Proceed with the FolderItem
It all depends on how much you want out of this function.
Some functions are project specific and I don’t get in a tizzy about being exacy about it. However, some things start alerting me that I want it to be part of my personal framework (something I suggest everyone has). In that case, I always align the function to some rules, like I always make it a function and most of the time I return an error code that is an integer. Most of the time I return FAIL or SUCCEED, but in other cases I make the error code (and BTW it doesn’t have to be an error but sometimes additional information) negative and I set a string in a personal LastError function, so someone can call it and see a custom description of what it was.
I know this was beyond your OP, but taking some time and setting up rules or a framework for yourself can save you time and effort on how to write functions in a way that provides excellent backup if something goes wrong.
Of course you do. Don’t know what I was thinking. Time to take some vacation.
I hear Vegas in March is nice…
Mid 70’s & the pools apparently open
And there’s a really handy liquor store just across the street and down the way a bit
now ask herself why would norm know that ???
Are we back on the subject of bribes?