Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"Unassigned Local Variable"

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

    "Unassigned Local Variable"

    I am getting a frustrating error "Unassigned Local Variable". I searched the forum and reviewed all related postings but don't see a solution. I have a class with two methods listed below. In the first method "update" I declare a built in type "bool retVal" and can use the variable just fine. In the second method I declare local variable "BarData fooVal". "BarData" is a valid class and I'm just trying to instantiate a local instance of this class. If I move the declaration "BarData fooVal" up to the data section of the class instead of within the method it also works fine. I don't see why built in types work differently than a class type? What am I missing?

    public bool update(double price public bool update(double price){
    bool retVal = false;
    _bar_cnt++;
    _bar_sum += price;
    _com_center.setBarData( _bar_sum / _bar_cnt, _bar_start.getBar() + (_bar_cnt/2) );
    if( _bar_sum > 10 ){ retVal = true; }
    // Check for price outside of channel
    return(retVal );
    }

    // Return center COM
    public BarData getCOM_center( POINT pt ){
    BarData fooVal;
    fooVal.setBarData(0,0);
    if( pt == POINT.BASE ){
    fooVal.setBarData(0, 0);
    return(fooVal);
    }else{
    fooVal.setBarData(0, 0);
    return(fooVal);
    }
    }
    bool retVal = false;
    _bar_cnt++;
    _bar_sum += price;
    _com_center.setBarData( _bar_sum / _bar_cnt, _bar_start.getBar() + (_bar_cnt/2) );
    if( _bar_sum > 10 ){ retVal = true; } // Setting retVal here works just fine.
    // Check for price outside of channel
    return(retVal );
    }

    // Return center COM
    public BarData getCOM_center( int pt ){
    BarData fooVal;
    fooVal.setBarData(0,0); // Unassigned Local Variable here.
    if( pt == 0 ){
    fooVal.setBarData(0, 0);
    return(fooVal);
    }else{
    fooVal.setBarData(0, 0);
    return(fooVal);
    }
    }
    Last edited by tradetree; 09-24-2010, 01:01 PM.

    #2
    Hello tradetree,

    This is related to C# more than NinjaScript so unfortunately outside our scope of support. Perhaps another community member can offer input on this.

    It also isn't clear, but it seems like you solved by moving the declaration outside the method?
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Yes, I have a workaround, but it means that I also have a global variable when it should be local. It is just poor programming practice. I can't understand it from a C# perspective, but I think you are right that it is indeed some kind of C# issue.

      Originally posted by NinjaTrader_RyanM View Post
      Hello tradetree,

      This is related to C# more than NinjaScript so unfortunately outside our scope of support. Perhaps another community member can offer input on this.

      It also isn't clear, but it seems like you solved by moving the declaration outside the method?

      Comment


        #4
        This was a programming error on my part. I needed to use "new" to construct the local object in C#. When I declared it a class variable, then it was constructed when the class object was constructed, which is why it worked in that case. Solved.

        Originally posted by tradetree View Post
        Yes, I have a workaround, but it means that I also have a global variable when it should be local. It is just poor programming practice. I can't understand it from a C# perspective, but I think you are right that it is indeed some kind of C# issue.

        Comment


          #5
          Thanks for the update and sharing your solution, tradetree.
          Ryan M.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          582 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          338 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          554 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          552 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X