I am having trouble understanding how to initialize a list of objects and use it with methods inside the class. I understand the List mechanism, but not how to initialize it inside the method and use it later.
For example, I want to have a class that creates a List when it is built. Then I want to use the method of this class to add items to the list. Elements in a list are objects defined in the SolidWorks API.
So, to build the list, I used:
public class ExportPoints : Exporter
{
public List<SldWorks.SketchPoint> listOfSketchPoints;
public ExportPoints(SldWorks.SldWorks swApp, string nameSuffix) :
base(swApp, nameSuffix)
{
List<SldWorks.SketchPoint> listOfSketchPoints = new List<SldWorks.SketchPoint>();
}
public void createListOfFreePoints()
{
try
{
this.listOfSketchPoints.Add(pointTest);
}
catch (Exception e)
{
Debug.Print(e.ToString());
return;
}
}
This is not executed at run time, as if listOfSketchPoints was never initialized as a List .
So, I tried to hack and it worked:
public ExportPoints(SldWorks.SldWorks swApp, string nameSuffix) :
base(swApp, nameSuffix)
{
List<SldWorks.SketchPoint> listOfSketchPoints = new List<SldWorks.SketchPoint>();
this.listOfSketchPoints = listOfSketchPoints;
}
, . , , .