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 Mindset, 04-21-2026, 06:46 AM
        0 responses
        90 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        137 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        68 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        120 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        72 views
        0 likes
        Last Post PaulMohn  
        Working...
        X