Reputation: 733
I have one fundamental question in object based programming in vb script or VBA. In general when we use the object oriented programming in Java we use object references to either a variable or function, like:
Obj.Add() or Obj.i=5
But in VBA and VB scripting we use stuff like Obj.Property.Property or Obj.Method.Property like:
Set sib = Tchilds.item(j).childNodes 'where item is a method defined in DOM.
My doubt how is this logically possible.
Upvotes: 1
Views: 196
Reputation: 38755
If you think of Properties as cousins of Subs/Functions/Methods (like getters/setters) then the mystery should vanish. The 'access object members' operator (dot) 'works' as long as on the left there is (a reference to) an object and on the right there is a valid member - method, variable/field, or property - name. Chaining dots 'works' as long as the x.y
expressions evaluate to an object, so that in y.z
z
is a member of that object. The last one may be a non-object (no further dot on its right).
>> Set oFS = CreateObject("Scripting.FileSystemObject")
>> Set oFld = oFS.GetFolder(".") -- method .GetFolder returns Folder object
>> Set oFiles = oFld.Files -- Property .Files returns collection object
>> WScript.Echo oFiles.Count -- oFiles has a simple/non-object member .Count
>>
2
>> WScript.Echo oFiles.Count.ToString() -- .Count is not an object, but a number
>>
Error Number: 424
Error Description: Object required
>> WScript.Echo oFS.GetFolder(".").Files.Count -- chaining
obj obj obj var
>>
2
Upvotes: 2