-1

I'm trying to use a list of GraphicsPath instead of an array as I don't know the number of the paths that will be created by user.

List<GraphicsPath> PathDB = new List<GraphicsPath>();

After this I fill the List as follows:

using(GraphicsPath myPath = new GraphicsPath())
{
   myPath.AddPolygon(myPoints);
   PathDB.Add(myPath);
}

But when I try to use the GraphicsPath from the list, while the Count property is correct, I cannot use the object like below because of an argument exception.

num = PathDB.Count;
for(int k=0; k < num; k++)
   {
      using(GraphicsPath myCurrentPath = new GraphicsPath())
      {
         myCurrentPath = PathDB[k];
         myCurrentPath.AddLine(0,0,400,400); //at this stage exception is thrown 
         myGraphics.DrawPath(myPen, myCurrentPath)
      }
   }

Is it something related to GraphicsPath being Disposabe ?? Or doing smt wrong?

Kory
  • 3
  • 2
  • 1
    What does the exception say? And yes, the object is disposed so you shouldn't use it any more. What are you trying to achieve with it? – Patrick Hofman Dec 18 '17 at 09:30
  • 1
    That *using* statement makes no sense whatsoever. You of course must not destroy anything that is stored in that list, that has to be done when it is *removed* from the list. C# is smart enough to not wreak havoc, but you probably doing that somewhere else as well. – Hans Passant Dec 18 '17 at 09:35
  • I don't get it. Why do you create a new instance of `GraphicsPath` within your loop, overwrite the variable with an existing value (which you have previously disposed) and then throw away the newly created value without using it? It seems you are not clear what `using` actually does (or what your code does for that matter). – Sefe Dec 18 '17 at 09:37

1 Answers1

3
using(GraphicsPath myPath = new GraphicsPath())
{
   myPath.AddPolygon(myPoints);
   PathDB.Add(myPath);
} // this disposes myPath

This is a local graphics path. Your using block Disposes it after the scope if done. So you need to remove that using block and instead dispose your paths when you no longer need them.

nvoigt
  • 75,013
  • 26
  • 93
  • 142