Data Danger Posted March 3, 2005 Posted March 3, 2005 Hi Could somebody help with the following I have written a program that uses the following code in a Scroll_ValueChanged section Dim Lineir1 As New Rectangle(l(0), t(0), w(0), h(0)) drwLineSection.FillRectangle(LineHatch, Lineir1) How does .net treat this variable Lineir1? It does not have a dispose command so I cannot call one. Also because the Dimed variable is in a Scroll section this rountine can be called hundreds of times. Does .net keep creating a new Lineir1 variable each time? Would it be better if the variable was in the Form_Declarations section. Also if I have created String,Boolean,Int variables in the Declarations section as private what do I need to do the clear these from memory is this done automatically when I dispose of the form? Thank you for your time. Quote
Talyrond Posted March 3, 2005 Posted March 3, 2005 Why not create the rectangle directly in the function, you will have no issues with memory using this method. drwLineSection.FillRectangle(LineHatch, New Rectangle(l(0), t(0), w(0), h(0))) Quote
Administrators PlausiblyDamp Posted March 3, 2005 Administrators Posted March 3, 2005 As a rule if a variable is only needed inside one procedure declare it in that procedure rather than the class level, declaring things with a greater scope can cause problems later if two functions just 'happen' to both use the same variable name. A rectangle is a value type anyway and so will not be the garbage collectors problem, value types are allocated on the functions stack and cleaned up as the function exits. Only reference types (classes) will need to be disposed and even then they will be the exception rather than the rule. In most scenarios just let the garbage collector do it's stuff and don't worry. The only time you will normally need to get involved is if the class encapsulates an 'expensive' resource (db connection, file handle etc.) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.