Note that there are some explanatory texts on larger screens.

plurals
  1. POCompare objects and determine common type
    primarykey
    data
    text
    <p>Short Version: Are the comments in the code correct? I think they are incorrect and should be reversed.</p> <pre><code>public Type GetTestClass(Object left, Object right) { if (left == null || right == null) return null; Type leftClass = left.GetType(); Type rightClass = right.GetType(); Type testClass; if (leftClass.IsInstanceOfType(right)) { testClass = leftClass; if (!rightClass.IsInstanceOfType(left)) { // rightClass is a subclass of leftClass testClass = rightClass; } } else if (rightClass.IsInstanceOfType(left)) { testClass = rightClass; if (!leftClass.IsInstanceOfType(right)) { // leftClass is a subclass of rightClass testClass = leftClass; } } else { // The two classes are not related. testClass = null; } return testClass; } </code></pre> <p>I came across this code in a code-base that I am working on and I think the comments are incorrect. I believe that the comments <code>//rightClass is a subclass of leftClass</code> and <code>//leftClass is a subclass of rightClass</code> are incorrect. For example, if <code>leftClass</code> is of type <code>Object</code> and <code>rightClass</code> is of type <code>MyClass</code> (which inherits from Object) then this is the how I believe the code works...</p> <ol> <li><code>Object</code> is an instance of <code>MyClass</code> is FALSE</li> <li><code>MyClass</code> is an instance of <code>Object</code> is TRUE</li> <li><code>testClass</code> is set to <code>MyClass</code></li> <li>NOT (<code>Object</code> is an instance of <code>MyClass</code> is FALSE) is TRUE</li> <li><code>testClass</code> is set to <code>Object</code></li> <li><code>testClass</code> is returned which == <code>Object</code></li> </ol> <p>I believe this is the correct functionality... ultimately the code is attempting to return the super-most class (you could also say the common Base class) of the two classes, or null if they are not related. Using the example above (steps 1-5) isn't it actually correct to say that the leftClass is a SUPER class of rightClass... or, reversed, the rightClass is a SUBCLASS of leftClass?</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.
 

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