tags:

views:

47

answers:

4

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?

A: 

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

David
A: 

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?

Greg B
A: 

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

John Nolan
+2  A: 

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>();
Grant Crofton
It looks like it's creating new instance each time. And making them static helped me. Thank you all for answers.
Mindaugas