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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        47 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        23 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        33 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        51 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        42 views
        0 likes
        Last Post CarlTrading  
        Working...
        X