Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

delete IDrawObjects?

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

    delete IDrawObjects?

    I have a set of rays that I want to redraw at each bar update. I´ve red THIS on IDrawObjects and got this far:

    foreach (IDrawObject ray in DrawObjects)
    {
    if (ray.Tag.StartsWith(ID))
    {
    "here I´d like to delete"
    }
    }

    As the code implies, the rays are tagged with a common ID and an individual loop index. Since the amount of rays varies from each bar update, I can´t simply overwrite with the same tag.

    Simply put; how do I delete IDrawObjects?

    Kindly, Fredrik
    Last edited by FREEN; 09-18-2011, 02:20 PM.

    #2
    Fredrik, please see this page for how to remove draw objects - http://www.ninjatrader.com/support/h...drawobject.htm.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks Austin. This works fine but gives me an error used inside a foreach loop:

      "Error on calling 'OnBarUpdate' method for indicator 'ARBfourierSeries110917' on bar 83: Collection was modified; enumeration operation may not execute."

      Code used:

      foreach (IDrawObject ray in DrawObjects)
      {
      if (ray.Tag.StartsWith("tagRay"))
      {
      Print(ray.Tag.ToString());
      Print("----------------------");
      RemoveDrawObject("tagRay1");
      }
      }

      Where do I go wrong? Any suggestion on how I can use the "RemoveDrawObject" method in a loop?

      //Fredrik

      Comment


        #4
        Originally posted by FREEN View Post
        Thanks Austin. This works fine but gives me an error used inside a foreach loop:

        "Error on calling 'OnBarUpdate' method for indicator 'ARBfourierSeries110917' on bar 83: Collection was modified; enumeration operation may not execute."

        Code used:

        foreach (IDrawObject ray in DrawObjects)
        {
        if (ray.Tag.StartsWith("tagRay"))
        {
        Print(ray.Tag.ToString());
        Print("----------------------");
        RemoveDrawObject("tagRay1");
        }
        }

        Where do I go wrong? Any suggestion on how I can use the "RemoveDrawObject" method in a loop?

        //Fredrik
        When using foreach, you cannot modify the collection. That is standard C#.

        Instead, list the tags of the objects that you want to delete into an ArrayList, and then use a for loop to iterate through and delete the items that are listed in that ArrayList.

        Comment


          #5
          Thanks koganam. Ok, the foreach loop is read only. The tutorial examples confuses me though since they reset properties of IDrawObjects in foreach loops, as in the code bellow. Maybe there´s a difference in resetting and deleting here?! On the other hand deleting objects in an array isn´t standard C# either as far as I know. I´ll play around with you idea and some other loop constructs, thanks!

          // Loops through the DrawObjects collection
          foreach (IDrawObject draw in DrawObjects)
          {
          // Unlocks all draw objects for manual manipulation on the chart
          draw.Locked = false;
          }
          Last edited by FREEN; 09-19-2011, 06:15 AM.

          Comment


            #6
            Freen, there is a difference. In pseudo-code, you'll need to do something like this:
            Code:
            ArrayList objectsToDelete = new ArrayList();
            if (check to delete idrawobjects = true)
            {
                foreach (IDrawObject ray in DrawObjects)
                {
                    if (ray.Tag.StartsWith("tagRay"))
                    {
                        objectsToDelete.Add(ray)
                    }
                }
                
                foreach (IDrawObject obj in objectsToDelete)
                {
                    RemoveDrawObject(obj.Tag);
                }
            }
            AustinNinjaTrader Customer Service

            Comment


              #7
              Originally posted by FREEN View Post
              Thanks koganam. Ok, the foreach loop is read only. The tutorial examples confuses me though since they reset properties of IDrawObjects in foreach loops, as in the code bellow. Maybe there´s a difference in resetting and deleting here?! On the other hand deleting objects in an array isn´t standard C# either as far as I know. I´ll play around with you idea and some other loop constructs, thanks!

              // Loops through the DrawObjects collection
              foreach (IDrawObject draw in DrawObjects)
              {
              // Unlocks all draw objects for manual manipulation on the chart
              draw.Locked = false;
              }
              First of all, C# provides methods for removing items from an ArrayList or Array. However, in this case, you will not be deleting the items in the ArrayList: the ArrayList is just a table of things to delete; it is never modified.

              In effect:
              1. Create a list of objects to be deleted
              2. Read the list and delete the corresponding object in a collection space. The list remains the same: nothing is deleted; the list is just something you are reading, not modifying.
              3. Once you have gone through the list, remove the list from memory.

              Yes, modifying the properties of objects in a collection is not the same as modifying the collection itself. It is a rather subtle difference, I agree.

              Comment


                #8
                Code:
                ArrayList objectsToDelete = new ArrayList();
                    foreach (IDrawObject ray in DrawObjects)
                    {
                        if (ray.Tag.StartsWith("tagRay"))
                        {
                            objectsToDelete.Add(ray)
                        }
                    }
                 
                    foreach (IDrawObject obj in objectsToDelete)
                    {
                        RemoveDrawObject(obj.Tag);
                    }

                Worked like a charm!

                In case more newbies read this, here´s some extras I learned from this:

                Pseudocode: "... intended for human reading rather than machine reading."

                Deleting in arrays: Arrays have a code defined number of "buckets". The contents can be changed/cleared, but the "bucket" itself can not... at least not without resizing in code. ArrayLists have a dynamic amount of "buckets" and don´t need explicit resizing.

                Thanks Austin and koganam!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by wildleanketosale, Today, 03:49 AM
                0 responses
                1 view
                0 likes
                Last Post wildleanketosale  
                Started by wildleanketosale, Today, 03:48 AM
                0 responses
                3 views
                0 likes
                Last Post wildleanketosale  
                Started by hiltonvarai, Today, 03:10 AM
                0 responses
                1 view
                0 likes
                Last Post hiltonvarai  
                Started by afyuwi, Today, 02:33 AM
                0 responses
                2 views
                0 likes
                Last Post afyuwi
                by afyuwi
                 
                Started by wushutrades, Today, 01:41 AM
                0 responses
                5 views
                0 likes
                Last Post wushutrades  
                Working...
                X