Note that there are some explanatory texts on larger screens.

plurals
  1. POVB Compiler does not do implicit casts to Object?
    primarykey
    data
    text
    <p>I've recently had a strange issue with one of my APIs reported. Essentially for some reason when used with VB code the VB compiler does not do implicit casts to Object when trying to invoke the ToString() method.</p> <p>The following is a minimal code example firstly in C# and secondly in VB:</p> <pre><code> Graph g = new Graph(); g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); foreach (Triple t in g.Triples) { Console.WriteLine(t.Subject.ToString()); } </code></pre> <p>The above compiles and runs fine while the below does not:</p> <pre><code> Dim g As Graph = New Graph() g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl") For Each t As Triple In g.Triples Console.WriteLine(t.Subject.ToString()) Next </code></pre> <p>The second VB example gives the following compiler exception:</p> <blockquote> <p>Overload resolution failed because no accessible 'ToString' accepts this number of arguments.</p> </blockquote> <p>This appears to be due to the fact that the type of the property <code>t.Subject</code> that I am trying to write to the console has explicitly defined <code>ToString()</code> methods which take parameters. The VB compiler appears to expect one of these to be used and does not seem to implicitly cast to Object and use the standard <code>Object.ToString()</code> method whereas the C# compiler does.</p> <p>Is there any way around this e.g. a VB compiler option or is it best just to ensure that the type of the property (which is an interface in this example) explicitly defines an unparameterized <code>ToString()</code> method to ensure compatability with VB?</p> <h2>Edit</h2> <p>Here are the additional details requested by Lucian</p> <ol> <li>Graph is an implementation of an interface but that is actually irrelevant since it is the <a href="http://www.dotnetrdf.org/api/index.asp?Topic=VDS.RDF.INode" rel="nofollow">INode</a> interface which is the type that <code>t.Subject</code> returns which is the issue.<br> <a href="http://www.dotnetrdf.org/api/index.asp?Topic=VDS.RDF.INode" rel="nofollow">INode</a> defines two overloads for <code>ToString()</code> both of which take parameters</li> <li>Yes it is a compile time error</li> <li>No I do not use hide-by-name, the API is all written in C# so I couldn't generate that kind of API if I wanted to</li> </ol> <p>Note that I've since added an explicit unparameterized <code>ToString()</code> overload to the interface which has fixed the issue for VB users.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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