Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Default Function - How to return an object

21 views
Skip to first unread message

vins

unread,
Jan 9, 2013, 8:00:29 PM1/9/13
to
Hi,
Is there any way to return an object via default function of a class?

Please see this example ...

Class myClass

Dim myVar

Private Sub Class_Initialize()
Set myVar = CreateObject("Scripting.Dictionary")
End Sub

Public Default Function myDefaultFunction
Set myDefaultFunction = myVar
End Function

End Class


Set myInstance = New myClass
msgbox myInstance.Count ' It throws an error. I thought it would show 0
msgbox myInstance.myDefaultFunction.Count ' It shows 0 which is as expected

Mayayana

unread,
Jan 9, 2013, 8:31:42 PM1/9/13
to
It would be better if you describe what you're
actually trying to do. You can pass objects
from a class, including another class, but passing
a Dictionary wouldn't make sense. The whole
point of a class is to wrap and simply functionality.
Passing a Dictionary would just be adding extra
complexity for no reason.

--
--
"vins" <vino...@gmail.com> wrote in message
news:5e911f1d-72ed-4608...@googlegroups.com...

Mayayana

unread,
Jan 9, 2013, 11:32:44 PM1/9/13
to
Here's an example of a way to return an object.
If you run this code you'll see that it gets rather
complicated. The FSO instance passed to the script
is the same one created in the class. When the class
terminates it should kill that instance. It does
release it, but the main script has created another
reference, as can be seen by the fact that the 2nd
MsgBox call works to return the %TEMP% path.

'-----------------------------------------------
Dim C1, C1Ob
Set C1 = New Cls1
Set C1Ob = C1.GetFSO
MsgBox C1Ob.GetSpecialFolder(1)
Set C1 = Nothing
MsgBox C1Ob.GetSpecialFolder(2)

Class Cls1
Private FSO

Public Function GetFSO()
Set GetFSO = FSO
End Function

Sub Class_Initialize()
Set FSO = CreateObject("Scripting.FileSystemObject")
End Sub

Sub Class_Terminate()
Set FSO = Nothing
End Sub

End Class
'---------------------------------------

Here's another version that can be useful. A class returns
another class. That can be useful when you need to do something
like organize a lot of data. For instance, a Class named CDisk
might have a function GetFolder, which could return a class
wrapper that provides easy access to a files collection, subfolders
collection, etc., just as FSO does. By populating the new class
with folder data before passing it on the file system can be
presented as an object model.

'------------------------------
Dim C1, C1Ob
Set C1 = New Cls1
Set C1Ob = C1.GetClass2
MsgBox C1Ob.GetTime

'-------------------
Class Cls1
Private Class2

Public Function GetClass2()
Set GetClass2 = Class2
End Function

Sub Class_Initialize()
Set Class2 = New Cls2
End Sub

Sub Class_Terminate()
Set Class2 = Nothing
End Sub

End Class
'--------------
Class Cls2

Public Function GetTime()
GetTime = Now()
End Function

End Class
'-------------------------------


vins

unread,
Jan 10, 2013, 11:22:42 AM1/10/13
to
Hi Mayayana,

Thanks for taking sometime to look into this post.

I understand the code you have posted and I know how to return an object from a class.

But, I would like to know If there is a way to return it from 'DEFAULT' function of the class. Ie, w/o calling the function name of the class as it is Default.

Actually it is kind of difficult to explain what i would like to achieve. But if above example is clarified, I would be able to do what i wanted to do.

I am still curious, why the below code throws error.

msgbox myInstance.Count ' Since it is default function, should it not return the dictionary object here?

Mayayana

unread,
Jan 10, 2013, 1:54:36 PM1/10/13
to
| I am still curious, why the below code throws error.
|

I see what you mean. I didn't realize that making it
the default function was an important part of what you
wanted to do.

This works as expected:

'------------------------------------------------
Set myInstance = New myClass
MsgBox myInstance

Class myClass

Public Default Function GetTime()
GetTime = Now()
End Function

End Class
'--------------------------------------------------

It returns the current time.


This doesn't work:

Set myInstance = New myClass
Set Dic = MyInstance
MsgBox Dic.Count

Nor this:

Set myInstance = New myClass
MsgBox myInstance.count


"By law" it should work, but the result makes sense.
In both versions the object referenced is ambiguous.
If the default function returns an object then there's
no way to reference the original object!

For instance, if you could do:

Set myInstance = New myClass
MsgBox myInstance.count

Then you wouldn't be able to do:

x = myInstance.Method2

WSH would report an error because Dictionary has
no Method2 function.

It's not for me to say how you should code, but I
think you'll find that most people avoid default methods.
They don't save any appreciable work but do cause
a lot of confusion, and they're only usable at all when
both the author and anyone working with the code later
are familiar with the object methods.

In VB6 there is a similar option and all controls have
default methods. The default property of a Textbox is
Text. That's the only one I know. :) Because in 12+ years
of using VB6 I've never used default methods/properties.
If I did I'd have to always study the context when an
object variable is used alone, to see whether it references
the object or the default property. To what purpose? I only
achieve the ability to avoid typing the word "text".


Mayayana

unread,
Jan 10, 2013, 2:10:19 PM1/10/13
to
| "By law" it should work, but the result makes sense.
| In both versions the object referenced is ambiguous.
| If the default function returns an object then there's
| no way to reference the original object!
|

Another way of looking at that: Since Obj can reference
either the object itself or the default property, the
interpreter can only act based on context. So x = Obj would
indicate the default property (or an error) because Set
wasn't used. Set x = Obj has to be passing the reference
to the object Obj itself. There's no other way for the interpreter
to discern which is which.


vins

unread,
Jan 10, 2013, 2:49:43 PM1/10/13
to
awesome explanation...
Very clear now..
Thank you so much!
0 new messages