Results 1 to 7 of 7

Thread: #Region's

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    #Region's

    Hi everyone,

    In the past, I've always left the generated #region part of a vb.net app, but I've just realised it's a good way of grouping functions which I want to hide.

    I've got the following code in several of my classes - 2 or 3 procedures which would be to hide / collapse & move out of the way at once.

    As I can't find diddly squat on custom #regions on the net, I want to ask if this is good practice or not? will the GC read the procedures ok if their held within a custom region? Thanks!

    VB Code:
    1. #Region "CleanupCode"
    2.     Public Overloads Sub Dispose()Implements IDisposable.Dispose
    3.         'Some Code Here
    4.     End Sub
    5.  
    6.     Protected Overrides Sub Finalize()
    7.         'More Code Here
    8.     End Sub
    9. #End region

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    I do this as well. I've had no problems with the GC not finding stuff in a hidden Region.
    This world is not my home. I'm just passing through.

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Everything starting with a # is a preprocessor directive..in #Region's case it means that it will just be ignored by the compiler, but will help IDE's provind the hide code functionality. If you think about it I wouldn't see a SINGLE good thing in creating members that would be hidden from the GC? What were you thinkin of? Why to have functions that wouldn't be compiled/read/whatever?
    \m/\m/

  4. #4

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    That's what I was after, some kind of explanation as to what the #region's are.

    I've just modified the framework garbagecollection sample & placed several of the disposed / finalize methods in regions & put extra writelines in to test that last theory & it seems to see them okay...

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I've just been doing some testing and kind of answered 1 point there, the region code compiles & runs okay like this:
    VB Code:
    1. Public Class Class1
    2.     implements IDisposable
    3.  
    4.     #region "Test"
    5.         Public Sub New()
    6.             console.WriteLine ("Created")
    7.         End Sub
    8.  
    9.         Public Overloads Sub Dispose() implements IDisposable.Dispose
    10.             GC.SuppressFinalize(Me)
    11.             console.WriteLine ("Disposed")
    12.         End Sub
    13.  
    14.         Protected Overrides Sub Finalize()
    15.             MyBase.Finalize()
    16.             console.WriteLine ("Finalized")
    17.         End Sub
    18.     #End region
    19.  
    20. End Class
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, _
    2. ByVal e As System.EventArgs) Handles Button1.Click
    3.     Dim x as New Class1
    4.     x.Dispose
    5.     x = nothing
    6. End Sub
    7.  
    8. Private Sub Button2_Click1(ByVal sender As Object, _
    9. ByVal e As System.EventArgs) Handles Button2.Click
    10.         Dim x as New Class1
    11.         ActiveForm.Dispose  
    12. End Sub
    Can anyone please provide a link or explanation of when to use regions, their purpose etc. though as I'd like to know more about them. Cheers!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    Regions are just ways of grouping related code in the editor - they have no impact on the compiled program.

    My own preference is to have a region for public constructors, methods, events etc. so the code looks like the help file...

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    To prove there is no impact, look at the visual studio created code when you design a form. It is all included in a region, but runs like normal. Regions ONLY allow you to collapse code groups to help you manage large amounts of code. It does ABSOLUTELY NOTHING other than collapse and expand code that you put inside it.

    That is it. There should be no more discussion about this.... This is actually silly this topic went this far.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width