Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error CS0161-not all code paths return a value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Error CS0161-not all code paths return a value

    private double calculateRange(double High1, double High2, double Low1, double Low2)
    {

    if( Low1<Low2)
    return High1-Low1;
    if( Low2<Low1)
    return High1-Low2;
    if( High1<High2)
    return High2-Low1;
    if( High2<High1)
    return High1-Low1;

    }

    When I try to compile it gives me 2 errors "cso161 not all code paths return a value" pointing me to the parts in bold. Could somebody please explain what that means and how to fix it?

    #2
    Hello Rdaoogle,

    Thank you for your post.

    When doing a method that returns a value you need to have at least one of the conditions be true.

    Since, all of these conditions could in theory be false, there is nothing to return a value.

    You can out a ELSE statement at the end to return a value to show that none of the conditions were met.

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by rdaoogle View Post
      private double calculateRange(double High1, double High2, double Low1, double Low2)
      {

      if( Low1<Low2)
      return High1-Low1;
      if( Low2<Low1)
      return High1-Low2;
      if( High1<High2)
      return High2-Low1;
      if( High2<High1)
      return High1-Low1;

      }

      When I try to compile it gives me 2 errors "cso161 not all code paths return a value" pointing me to the parts in bold. Could somebody please explain what that means and how to fix it?
      You need a final return directive/value if all your if blocks fall through, using either an else block or an unconditional return.

      Comment


        #4
        yep, that fixed it.
        Thank you both!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X