Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 usazencort, Today, 01:16 AM
          0 responses
          1 view
          0 likes
          Last Post usazencort  
          Started by kaywai, 09-01-2023, 08:44 PM
          5 responses
          603 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          6 responses
          22 views
          0 likes
          Last Post xiinteractive  
          Started by Pattontje, Yesterday, 02:10 PM
          2 responses
          20 views
          0 likes
          Last Post Pattontje  
          Started by flybuzz, 04-21-2024, 04:07 PM
          17 responses
          230 views
          0 likes
          Last Post TradingLoss  
          Working...
          X