Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two issues with this code, </p> <p>First off you're throwing an exception, followed by a return - the return statement will never be hit as the exception will stop execution of the rest of the method, making the return statement superfluous. </p> <p>Secondly, you can't return null when the return type is float; you'd have to change the return type to be float? (see: <a href="http://msdn.microsoft.com/en-us/library/1t3y8s4s%28v=vs.80%29.aspx" rel="noreferrer">nullable types</a>) </p> <p>So either, if it is a real error case, as there is nothing you can do go with just the exception: </p> <pre><code> public float GetMaxX() { if(RawData == null) throw new NullReferenceException(); //DO other stuff to find max X return MAX_X; //as float } </code></pre> <p>Or alternatively, return the null and drop the exception:</p> <pre><code> public float? GetMaxX() { if(RawData == null) return null; //DO other stuff to find max X return MAX_X; //as float } </code></pre> <p>Personally, if <code>RawData</code> being null is an error condition / exceptional circumstance that should never happen then I would say throw the exception, and handle the exception if thrown in the calling code. </p> <p>An alternative approach would be to force initialisation of <code>RawData</code> through the constructor, make <code>RawData</code> private (or at least the setter) and throwing the exception there. Leaving any other logic within the class clean of any exception throwing / null-checking as it can assume that <code>RawData</code> will have been set previously.</p> <p>Resulting in something along the lines of:</p> <pre><code>public class DataPoints { private readonly PointF[] rawData; //raw measurement pairs public float xMax; //max value on X axis public float yMax; //max value on Y axis public DataPoints(PointF[] rawData) { if (rawData == null) throw new ArgumentNullException("rawData"); this.rawData = rawData; } public float GetMaxX() { //DO other stuff to find max X return MAX_X; //as float } } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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