开发者

Data persistance in class library between one class methods

I am creating a class library for AutoCAD with .NET.

The problem is that the methods are called one after another from AutoCAD and first one reads input file and creates List of data in memory. However when the new one is called the list is empty.

I need to find a solution how to keep that data. The List contains data in my created structure. Methods are called independently, but in order.

Short code example:

namespace GeoPjuvis
{
   ...

public class Program
{
    ...

//program variables 
private List<GeoData> dataList;开发者_运维问答
private List<DataPoint> points;
private int mapScale;

public Program()
{
    dataList = new List<GeoData>();
    points = new List<DataPoint>();
}

//Initialization method of the program. Makes praperations. Reads files. Add points to map.
[CommandMethod("geoinit", CommandFlags.Session)]
public void Init()
{
    ...
}

//method uses data gathered before and selects points  
[CommandMethod("selectPoints", CommandFlags.Session)]
public void SelectPoints()
{

    ...

}...

So why these dataList and points lists are empty when I call SelectPoints() method. And how to avoid that?


I don't know about programming for AutoCAD, but I'd suspect that it's creating a new instance each time. You could try making the variables static (e.g. class-level):

private static List<GeoData> dataList = new List<GeoData>();


Is it instantiating a new class each time it calls a method? (Forgive me, I'm not familiar with coding for AutoCAD.) Try making the class static. If that doesn't work, can you return the value(s) from the first method to AutoCAD and have it send those as arguments to the next method? That wouldn't be the best solution for performance, keep in mind.

Also, for reference, take a look at the a Singleton implementation in C#:

http://msdn.microsoft.com/en-us/library/ff650316.aspx


At a guess, based on the information you've given, does AutoCAD create a new instance of your object for each method call? This would explain why your instance variables are empty.

Try making the variables static and see if the data persists across method calls.

Does the AutoCAD docs have any instructions for writing these programs?


It looks like you are calling a new instance of your class, You could implement a singleton pattern to make sure you are always calling the same instance or persist the points and load them second time round.

Here's a good link for the Singleton implementation in c#, http://csharpindepth.com/Articles/General/Singleton.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜