Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I used VarType(y). The result is 8, for vbString. It should be 9 for object. – ssorrrell 1 hour ago</p> <p>Use Print y in the Immediate window to find the contents of y. – ssorrrell 55 mins ago</p> </blockquote> <p>This seems to confirm my suspicions. The <code>MyOwn.Object</code> class must have a default property or method that returns a string. </p> <p>Therefore, when you try to <code>Debug.Print</code> it, it will return the value of the default property/method. When you hover over the variable in the IDE, VB6 will display the value of the default property/method. When you do a <code>VarType</code> call on <code>y</code> it will return the variable type of the default property or method.</p> <p>The reason is that when you have a variable of type <code>Variant</code> that stores an <code>Object</code>, and the class of the object defines a default method or property, the variable will evaluate to the return value of the default method or property in most situations. </p> <p>You can quickly check to see if the <code>MyOwn.Object</code> class has a default member by opening the <em>Object Browser</em> to the <code>MyOwn.Object</code> class and looking at the its list of properties and methods. If you see a method or property that has an icon with small blue circle in the corner, that indicates the method or property is the default member of the class. If you find one, I'm willing to bet it's declared to return a string.</p> <p>Note that even if you changed all your <code>Variant</code>S to <code>Object</code>S, you would still encounter this behavior in a number of places. For example, even if <code>y</code> is declared <code>As Object</code>, doing a <code>Debug.Print y</code> will still print out the value of the default property or method, and doing a <code>VarType(y)</code> will still return 8 (string).</p> <p>Knowing exactly when VB6 will use the default member and when it won't can be confusing. For example, if you declare <code>y</code> as <code>Object</code>, then doing <code>TypeName(y)</code> will return <code>MyOwn.Class</code>, but <code>VarType(y)</code> will still return 8 (string). However, if you declare <code>y</code> as <code>Variant</code>, then <code>TypeName(y)</code> returns <code>String</code>.</p> <p>If you are using late-binding, it's hard to avoid this side-effect, since you'll only be able to declare your object variable as <code>Object</code> or <code>Variant</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload