Results 1 to 8 of 8

Thread: [RESOLVED] [CodeDom] Putting code in a region directive?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Resolved [RESOLVED] [CodeDom] Putting code in a region directive?

    According to the MSDN documentation, you create a #Region with the CodeRegionDirective class... but it is not obvious to me how to put code inside that region?

    I want to generate:-
    Code:
    #Region "Private members"
      Private Readonly m_Id As Integer
    #End Region
    Any ideas?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [CodeDom] Putting code in a region directive?

    That's exactly how you do it... in fact, when ever I used to build a class first thing I did was stub out my regions:
    Code:
    #Region "Private members"
    
    #end Region
    
    #Region "Properties"
    
    #end Region
    
    #Region "Private Functions"
    
    #end Region
    
    #Region "Private Methods"
    
    #end Region
    
    #Region "Public Functions"
    
    #end Region
    
    #Region "Public Methods"
    
    #end Region
    
    #Region "Event Handlers"
    
    #end Region

    I don't do that anymore because it's a maintenance nightmare... but you just simply create a region and stuff code into it. It is strictly an IDE organizational tool... nothing more.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [CodeDom] Putting code in a region directive?

    Ah - but how do I make this happen in CodeDOM? I can put a region around a whole class by adding these to the .StartDirectives and .EndDirectives of the CodeTypeDeclaration - but how to do this for just a collection of CodeStatement?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [CodeDom] Putting code in a region directive?

    I'm fairly sure the easiest way is to just include them in the code you pass to the compiler...

  5. #5
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: [CodeDom] Putting code in a region directive?

    You only run the code with codedom. You don't actually see it in the IDE so what is the point of the #Region statements with codedom?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [CodeDom] Putting code in a region directive?

    My bad, I didn't see that this was the through the CodeDom ... although I can't imagine it's all that different. As I mentioned, regions are a trick for the IDE, so the compiler is going to ignore them. If you're generating code that no one's really going to see, then I don't think there's much point in including them.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [CodeDom] Putting code in a region directive?

    The tool I am writing is going to create code that people are going to see - it will write the partial classes from the model (as XML) that makes the framework for the system which the developers then augment with their own code. (Like entity framework does etc.)

    It appears I have to attaché the "start region" to the first CodeStatement in the collection and the "end region" to the last statement in the collection. I might just write a utility function for that :-)

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [CodeDom] Putting code in a region directive?

    OK - simply adding the region directives to the first and last in the collection of code lines seems to work:-

    VB Code:
    1. Public Shared Sub WrapInRegion(ByVal RegionText As String, ByVal codeLines As ICollection(Of CodeTypeMember))
    2.  
    3.         If (codeLines.Count > 0) Then
    4.             codeLines(0).StartDirectives.Add(New CodeRegionDirective(CodeRegionMode.Start, RegionText))
    5.             codeLines(codeLines.Count - 1).EndDirectives.Add(New CodeRegionDirective(CodeRegionMode.End, String.Empty))
    6.         End If
    7.  
    8.     End Sub

    This results in
    VB Code:
    1. '------------------------------------------------------------------------------
    2. ' <auto-generated>
    3. '     This code was generated by a tool.
    4. '     Runtime Version:4.0.30319.42000
    5. '
    6. '     Changes to this file may cause incorrect behavior and will be lost if
    7. '     the code is regenerated.
    8. ' </auto-generated>
    9. '------------------------------------------------------------------------------
    10.  
    11. Option Strict Off
    12. Option Explicit On
    13.  
    14.  
    15. Namespace test
    16.    
    17.     Partial Public Class Duncan_s_Interface
    18.        
    19.         #Region "Private members"
    20.         Private _Email As String
    21.        
    22.         Private _Height_cm As Integer
    23.         #End Region
    24.     End Class
    25. End Namespace
    Last edited by Merrion; Aug 24th, 2015 at 04:04 AM.

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