Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>AS. the answer was extended few times, as more and more info was uncovered turning the question from "what is CType function?" to "How to convert OleVariant to a required Interface type?". Thus the answer gradually covers all those topics.</em></p> <p>So, you met unknown function in Microsoft Visual Basic code. What is one to do, when he meets something yet unknown? Go Google.</p> <p>Google.com with text <code>CType MSDN</code> gives us this link in its top results: <a href="http://msdn.microsoft.com/en-us/library/vstudio/4x2877xb.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/vstudio/4x2877xb.aspx</a></p> <blockquote> <p>Returns the result of explicitly converting an expression to a specified data type, object, structure, class, or interface. If no conversion is defined from expression to typename (for example, from Integer to Date), Visual Basic displays a compile-time error message.</p> </blockquote> <p>So we have to reproduce typecast in Delphi, preferably compile-type type-cast, if possible.</p> <p>Google.com with text <code>typecast docwiki</code> gives us this link in its top results: </p> <ul> <li><a href="http://docwiki.embarcadero.com/RADStudio/XE4/en/Expressions_(Delphi)#Typecasts" rel="nofollow noreferrer">http://docwiki.embarcadero.com/RADStudio/XE4/en/Expressions_(Delphi)#Typecasts</a></li> </ul> <p>Which in turn gives us one more link referenced in the text:</p> <ul> <li><a href="http://docwiki.embarcadero.com/RADStudio/XE4/en/Interface_References#Interface_Typecasts" rel="nofollow noreferrer">http://docwiki.embarcadero.com/RADStudio/XE4/en/Interface_References#Interface_Typecasts</a></li> </ul> <p>So you have two syntaxes to try. One you tried and ruled out - the one with AS operator. Then try another, direct typecast syntax. </p> <pre><code>stylesheet := IHTMLStyleSheet( document.styleSheets.item(0) ); </code></pre> <p>Sometimes if above fails it also helps doing double typecast, <code>Variant -&gt; IUnknown -&gt; certain interface</code>, but in many cases that is but redundant version of the upper code.</p> <pre><code>stylesheet := IHTMLStyleSheet( IInterface( document.styleSheets.item(0) ) ); </code></pre> <p>Well, now that it was told that the source expression datatype is <code>OleVariant</code> we can read documentation with more precise aim, about <code>Variant</code> datatype conversions (<code>OleVariant</code> is little different from <code>Variant</code> in modern Delphi):</p> <ul> <li>AS operator is applicable to classes, objects and interfaces. It is not applicable to <code>Variant</code> expressions. This answers the original "why we cannot..." question.</li> <li>Documentation on <code>Variant</code> type suggests converting it using direct typecast syntax.</li> </ul> <p>.</p> <pre><code>stylesheet := IInterface( document.styleSheets.item(0) ) as IHTMLStyleSheet; </code></pre> <p>If wished, you can even use (over?)defensive programming using <a href="http://docwiki.embarcadero.com/Libraries/XE2/en/System.Variants.VarType" rel="nofollow noreferrer">http://docwiki.embarcadero.com/Libraries/XE2/en/System.Variants.VarType</a> to check that you got <code>varUnknown</code> or <code>varDispatch</code> before attempting at getting <code>IUnknown</code> out of the returned value.</p> <p>PS. The question seems as a duplicate now.</p> <ul> <li><a href="https://www.google.ru/search?q=delphi+cast+olevariant+to+interface" rel="nofollow noreferrer">https://www.google.ru/search?q=delphi+cast+olevariant+to+interface</a></li> <li><a href="https://stackoverflow.com/questions/4938601">Getting an IStream from an OleVariant</a></li> <li><a href="https://stackoverflow.com/questions/16111060">How to cast OleVariant to IDispatch derived?</a></li> </ul>
 

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