It. and List <T>

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
        {
            //imagine more code here
            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;
}

, . , , .

  • "" , ?
  • , ?
+4
6

, . .

List<SldWorks.SketchPoint> listOfSketchPoints  = new List<SldWorks.SketchPoint>();

this.listOfSketchPoints = new List<SldWorks.SketchPoint>();

, , : -)

+3

listOfSketchPoints . , .

this, . :

public ExportPoints(SldWorks.SldWorks swApp, string nameSuffix) :
    base(swApp, nameSuffix)
{
    this.listOfSketchPoints  = new List<SldWorks.SketchPoint>();
}
+1

, :

public ExportPoints(SldWorks.SldWorks swApp, string nameSuffix) :
    base(swApp, nameSuffix)
{
    List<SldWorks.SketchPoint> listOfSketchPoints  = ...
}

, . List<T> , . , .

, :

public ExportPoints(SldWorks.SldWorks swApp, string nameSuffix) :
    base(swApp, nameSuffix)
{
    this.listOfSketchPoints = new List<SldWorks.SketchPoint>();
}

, this. (, , ).

+1

:

this.listOfSketchPoints = new List<SldWorks.SketchPoint>()

listOfSketchPoints , .

+1
public ExportPoints(SldWorks.SldWorks swApp, string nameSuffix) :
    base(swApp, nameSuffix)
{
    this.listOfSketchPoints  = new List<SldWorks.SketchPoint>();
}

public List<SldWorks.SketchPoint> listOfSketchPoints = new List<SldWorks.SketchPoint>();

public ExportPoints(SldWorks.SldWorks swApp, string nameSuffix) :
    base(swApp, nameSuffix)
{
}
0

Perhaps you could output this code. It would be better when defining objects around the world for the particualr class it should be declared using a private keyword rather than an open keyword. Because another form or class may try to access the value of a variable that is specific to a particular class or program. (this becomes a security risk). Since the object is declared globally, it can be used in any methods within the same class. Check this code a bit.

public class ExportPoints : Exporter

{
private List<SldWorks.SketchPoint> listOfSketchPoints;

public ExportPoints(SldWorks.SldWorks swApp) :
    base(swApp)
{
     listOfSketchPoints  = new List<SldWorks.SketchPoint>();
}

public void createListOfFreePoints(string pointTest)
{
    try
    {
        //imagine more code here
        listOfSketchPoints.Add(pointTest);
    }
    catch (Exception e)
    {
        Debug.Print(e.ToString());
        return;
    }
}
0
source

Source: https://habr.com/ru/post/1542942/


All Articles