.NET

Reply
Distinguished Contributor
Dale.Bartlett
Posts: 151
Registered: ‎01-06-2003
Message 1 of 5 (310 Views)
Accepted Solution

Database parameter in batch process - by ref?

310 Views, 4 Replies
11-21-2012 02:43 AM

I have a master batch process which comprises several steps each of which will change the database. I am gettting the database once, then pass it thought the subsequent steps. Simple example in pseudo code:

void BatchAll(){

database db = new database

BindXref(db)

PurgeAll(db)

DoSomethingElse(db)

save dwg

}finish BatchAll

Should the database db be passed by reference (ref db)?

Thanks Dale

Probably the first thing you might want to do is research the difference between 'reference types' and 'value types' in .NET.  A database is a reference type, which means that it is implicitly passed 'byref' regardless of whether you use 'byref' or 'byval', so in fact, there is no point to using 'byref' with any reference type parameter.

 

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 2 of 5 (294 Views)

Re: Database parameter in batch process - by ref?

11-21-2012 12:25 PM in reply to: Dale.Bartlett

Probably the first thing you might want to do is research the difference between 'reference types' and 'value types' in .NET.  A database is a reference type, which means that it is implicitly passed 'byref' regardless of whether you use 'byref' or 'byval', so in fact, there is no point to using 'byref' with any reference type parameter.

 

 

Distinguished Contributor
Dale.Bartlett
Posts: 151
Registered: ‎01-06-2003
Message 3 of 5 (268 Views)

Re: Database parameter in batch process - by ref?

11-22-2012 01:02 AM in reply to: DiningPhilosopher

Thanks, very helpful.  Regards, Dale

Distinguished Contributor
Dale.Bartlett
Posts: 151
Registered: ‎01-06-2003
Message 4 of 5 (216 Views)

Re: Database parameter in batch process - by ref?

11-29-2012 12:31 AM in reply to: Dale.Bartlett

So given the following senario:

DoAllThisStuff()

{

  DoThis1()

  DoThis2()

  DoThis3()

  DoThis4()

}

Should DoAllThisStuff() pass db as a  parameter to each function, or should each one set db to Active Doc separately?

Database db = MdiActiveDocument.Database;

Best practice?

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 5 of 5 (191 Views)

Re: Database parameter in batch process - by ref?

12-07-2012 05:16 AM in reply to: Dale.Bartlett

A best practice is to avoid any referencing of the 'active' document's database or assumption that your code is operating on the active document's database.

 

Pass a database or any database-resident object (or ObjectId) in the database to your functions, because that allows them to be used in more situations (like for example, databases that are not open in the AutoCAD editor).

 

You can pass database-resident objects and ObjectIds as well, when appropriate, since the containing database is easily accessed from their Database property. 

 

Another best practice is to avoid starting or comitting transactions in called APIs. That is best done at the highest level (like for example, within a handler for a custom command).

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!