Author Topic: Help please, variables losing values moving between subs?  (Read 4136 times)

0 Members and 1 Guest are viewing this topic.

commodore

  • Newt
  • Posts: 51
Help please, variables losing values moving between subs?
« on: January 11, 2011, 01:15:50 PM »
This is my first really large Dot Net program and its giving me fits!
To try to simplify let me explain what it is supposed to do and then whats I believe is happening.
The program is supposed to take a number of drawings from a directory and make a collection of the filenames, open a new view drawing and place a certain number of these drawings in the new drawing until a specified area is filled, make a named view, then save and close the drawing, then open a new sheet drawing from a template, bring in the view drawing that was just created, create a paperspace viewport save and close. Then it would start the process over again beginning with the next filename from the collection (create view dwg, create sheet, create view dwg, create sheet) until all the files in the directory have been procesed.
So, I am doing this with with Sub_Routines and passing variables but it seems that as drawings are getting opened and closed my variables are, what seems to be randomly, getting reset to "nothing", I say randomly because sometimes the program will run and finish fine, but the next time I run it it errors out on the 2nd sheet (2nd run, 2nd sheet).
I think it might be how or where I have my variables declared or do my subs have to be "Shared" also to get the variables passing 100%?
Please give me some advice on what to do to solve this issue. I have attached the code. 

chrisp

  • Guest
Re: Help please, variables losing values moving between subs?
« Reply #1 on: January 11, 2011, 03:10:31 PM »
Have you been able to pinpoint a sub/function or program line where the program bombs out?

Chris

commodore

  • Newt
  • Posts: 51
Re: Help please, variables losing values moving between subs?
« Reply #2 on: January 11, 2011, 03:20:07 PM »
Have you been able to pinpoint a sub/function or program line where the program bombs out?

Chris

Yes, line 291 seems to be causing a problem for line 299
line 291:  ViewDoc = myAcadApp.DocumentManager.Add(TemplateDwgFullPath)
my variable "sPathView" used on line 299 is set when I step through this 3 times (3 drawings created fine), on the 4th loop through line 291, right after it executes ViewDoc = myAcadApp.DocumentManager.Add(TemplateDwgFullPath) "sPathView" suddenly becomes "nothing" causing an error on line 299.


jgr

  • Guest
Re: Help please, variables losing values moving between subs?
« Reply #3 on: January 11, 2011, 03:41:30 PM »
I believe that many instance variables could be local variables

For example:regCurUser

commodore

  • Newt
  • Posts: 51
Re: Help please, variables losing values moving between subs?
« Reply #4 on: January 11, 2011, 03:50:26 PM »
I believe that many instance variables could be local variables

For example:regCurUser

Please correct me if I am mistaken, and I probabaly am, but where I have that variable declared (outside of all subs/funcs/if-endifs etc) makes it global scope doesn't it? or do I need to have it declared with "Public" instead of "Dim"?

commodore

  • Newt
  • Posts: 51
Re: Help please, variables losing values moving between subs?
« Reply #5 on: January 11, 2011, 04:02:40 PM »
Have you been able to pinpoint a sub/function or program line where the program bombs out?

Chris

Im also having the variable "sPathView" after line 291 ViewDoc = myAcadApp.DocumentManager.Add(TemplateDwgFullPath) runs end up as a japanese kanji character string!
Cool eh!

jgr

  • Guest
Re: Help please, variables losing values moving between subs?
« Reply #6 on: January 11, 2011, 05:07:59 PM »
I must be wrong since I'm using notepad to read your code.

But, why you need to store the value of ViewDoc in an instance variable?
In my previous example: 
Dim regCurUser As RegistryKey
you've declared this variable as an "instance variable" (at modul level, in vb6), but it is only used in Sub What_Sheets. why not declare it locally?


Sorry i d not speack English

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Help please, variables losing values moving between subs?
« Reply #7 on: January 12, 2011, 12:34:20 AM »
Being that the code is 1000+ lines of code, and not having time to look at it in depth.


And not sure where or how it looping

Just right clicking on sPathView and finding all references I found one place in your code where it is set

line 171
Code: [Select]
sPathView = Path.Combine(sProjPath, "Views")


If I remeber correrctly if the following is nothing........ that can cause the previous to be nothing or an error

line 127
Code: [Select]
sProjPath = Path.GetDirectoryName(sCurrProj)

Shared is like static all instances of class1 will "share"  the same value.

Making a sub Shared means you do not have to create an instance to use that member method.
Class1.MethodName
 vs.
Dim cls1 as new Class1
cls1 .MethodName
 



commodore

  • Newt
  • Posts: 51
Re: Help please, variables losing values moving between subs?
« Reply #8 on: January 12, 2011, 07:28:06 AM »
Being that the code is 1000+ lines of code, and not having time to look at it in depth.


And not sure where or how it looping

Just right clicking on sPathView and finding all references I found one place in your code where it is set

line 171
Code: [Select]
sPathView = Path.Combine(sProjPath, "Views")


If I remeber correrctly if the following is nothing........ that can cause the previous to be nothing or an error

line 127
Code: [Select]
sProjPath = Path.GetDirectoryName(sCurrProj)

Shared is like static all instances of class1 will "share"  the same value.

Making a sub Shared means you do not have to create an instance to use that member method.
Class1.MethodName
 vs.
Dim cls1 as new Class1
cls1 .MethodName
 




Thank you for looking.
The looping begins inside the if statements starting on line 176 and the loops execute fine to a point so initially my variables are ok, the problem seems to be at line 291 (inside the looping) when a new document is created and opened. The program can loop through this line serveral times without losing or corrupting my variables but at the 4th time through they get messed up. 

kaefer

  • Guest
Re: Help please, variables losing values moving between subs?
« Reply #9 on: January 12, 2011, 08:04:37 AM »
The looping begins inside the if statements starting on line 176 and the loops execute fine to a point so initially my variables are ok, the problem seems to be at line 291 (inside the looping) when a new document is created and opened. The program can loop through this line serveral times without losing or corrupting my variables but at the 4th time through they get messed up. 

Am I at the right position?
Code: [Select]
            ViewDoc = myAcadApp.DocumentManager.Add(TemplateDwgFullPath) ' 291
            ViewDoc = myAcadApp.DocumentManager.MdiActiveDocument        ' 292

Without digging too deep into your code, here the red lamp lits up and says "Danger". I can't pinpoint the reason for this sinking feeling accurately, so random ramblings may follow.

DocumentCollection is a thin wrapper around AcApDocManager, the Add method wraps appContextNewDocument.
AcApDocManager::appContextNewDocument can only be called from the application context.

There's no .NET way to make a document current,  which is necessary to manipulate documents other then the topmost or active. See AcApDocManager::setCurDocument.

In short, it may be better to rethink your approach with using either COM automation (visible in drawing editor) or with a .NET side database (when not).

Regards

commodore

  • Newt
  • Posts: 51
Re: Help please, variables losing values moving between subs?
« Reply #10 on: January 12, 2011, 08:33:29 AM »
The looping begins inside the if statements starting on line 176 and the loops execute fine to a point so initially my variables are ok, the problem seems to be at line 291 (inside the looping) when a new document is created and opened. The program can loop through this line serveral times without losing or corrupting my variables but at the 4th time through they get messed up.  

Am I at the right position?
Code: [Select]
           ViewDoc = myAcadApp.DocumentManager.Add(TemplateDwgFullPath) ' 291
            ViewDoc = myAcadApp.DocumentManager.MdiActiveDocument        ' 292

Without digging too deep into your code, here the red lamp lits up and says "Danger". I can't pinpoint the reason for this sinking feeling accurately, so random ramblings may follow.

DocumentCollection is a thin wrapper around AcApDocManager, the Add method wraps appContextNewDocument.
AcApDocManager::appContextNewDocument can only be called from the application context.

There's no .NET way to make a document current,  which is necessary to manipulate documents other then the topmost or active. See AcApDocManager::setCurDocument.

In short, it may be better to rethink your approach with using either COM automation (visible in drawing editor) or with a .NET side database (when not).

Regards


This is an exerpt from the Developers Guide "Create and Open a Drawing"
  Dim acDoc As Document = acDocMgr.Add(strTemplatePath)
  acDocMgr.MdiActiveDocument = acDoc
So I do have the second line wrong but if I corrected it per the 2nd line above it looks like it would set the newly created drawing current right?

Also, on stepping through the code better I actually watched the "sPathView" variable change to "nothing" upon executing line 815:
SheetDoc = myAcadApp.DocumentManager.Open(sFile, False, "") on the 4th loop.
Continuing I then had an error on line 819: app.ActiveDocument.PurgeAll(). Cause is that variable "app" is now also = "nothing" (happened at the exact same time). Now I set app to this "app = CType(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, AcadApplication)" on line 77
« Last Edit: January 12, 2011, 09:43:39 AM by commodore »

commodore

  • Newt
  • Posts: 51
Re: Help please, variables losing values moving between subs?
« Reply #11 on: January 15, 2011, 10:20:17 AM »
Got it solved!
Came down to adding 2 lines of code after I saved and closed each drawing.

            SheetDoc = Nothing
            GC.Collect()

where "SheetDoc" is the drawing document object.
(Thank you Bruce)