Product : Engine, Version : 4.0.0.1017, ArticleID : 41017036

HowTo : Problems with .New and .Open methods inside loop in .NET enviroment.

Article41017036
TypeHowTo
ProductEngine
Version4.0.0.1017
Date Added3/20/2003
Submitted byBryan Kim
Keywords

Subject

Problems with .New and .Open methods inside loop in .NET enviroment.

Summary

The C# code below is a sample code that produces error(s) in .NET enviroment.

	private void buttonTest_Click(object sender, System.EventArgs e)
		{
			int count = 50;
			int countItem = 100;
			//1st new
			axVDPro.ActiveDocument.New(); // or after .Open
			VDProLib.vdLine vdLine = null;
			for(int i = 0; i < count ; i++)
			{
				bool check = axVDPro.ActiveDocument.New(); // or after .Open
				for (int k = 0; k <  countItem; k++)
				{
					vdLine = axVDPro.ActiveDocument.Entities.AddLine(
				      new object[] {100*k,0,0} , new object[] {100*k,1000,0} ) ;
				}
			}
			axVDPro.CommandAction.Zoom("E",0,0);
		}
	}

Solution

The reason is that .New and .Open methods inside loops release the old references and create new in Entities, Blocks, Layers, TextStyles, DimStyles and Selections collections and in Model object.
So when you call some of the above, you have to declare it in the start of the code and you have to update its value after the .New or .Open methods.

The above code should be like :

	private void buttonTest_Click(object sender, System.EventArgs e)
	{
		int count = 50;
		int countItem = 100;
		VDProLib.vdEntities ents;
		ents = axVDPro.ActiveDocument.Entities;
		//1st new
		axVDPro.ActiveDocument.New();
		ents = axVDPro.ActiveDocument.Entities;
		VDProLib.vdLine vdLine = null;
		for(int i = 0; i < count ; i++)
		{
			bool check = axVDPro.ActiveDocument.New();
			ents = axVDPro.ActiveDocument.Entities;
			for (int k = 0; k <  countItem; k++)
			{
				vdLine = ents.AddLine( new object[] {100*k,0,0} , 
				        new object[] {100*k,1000,0} ) ;
			}
		}
		axVDPro.CommandAction.Zoom("E",0,0);
	}

This solution is the same for Standard component. In that case the VDProLib should be replaced by VDStdLib.