Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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)))

  • Administrators
Posted

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.)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...