Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

How To Create Regions In Methods

Hey everyone! I found out about this cool little trick on how to organize your code so it is less confusing. This is great when you are working on a large project & you want to be able to keep track of what you have done.

As most of you already know, in C# you are able to use #Regions in Subs & Functions. This, however, is not the case in VB.Net. This little tip is an alternative to #Regions & I hope that you guys find this tip useful.




The Method
1. Open your project or .vb file in Visual Studio.
Example (Click to View)

2. Highlight the text that you would like to be in the Region.
Example (Click to View)

3. Press Ctrl + M & then Ctrl + H. The text will then be in a Region.
Example (Click to View)

4. To expand or collapse the region, click the + or - button in the margin.
Example (Click to View)

5. To make things easier, you can add a comment above the new Region as a reference.
Example (Click to View)




I hope that you find this little tip helpful. Sorry if this has been posted before. Post below if you have other tips that have helped you & that other people might find helpful.
Quote:As most of you already know, in C# you are able to use #Regions in Subs & Functions. This feature, however, is not available in VB.Net. This little tip is an alternative to #Regions & I hope that you guys find this tip useful.


dunno where you got your info from there because you have always been able to use regions in vb
(Jul 3, 2015 08:40 AM)nathan72389 Wrote:  dunno where you got your info from there because you have always been able to use regions in vb

As I said, you cannot use Regions in a Sub or Function.

[Image: oobPoGF.png]
Wow. Learned something new. Thanks!
Umm... You can use regions in Vb.net but just not in Subs

i.gyazo.com/93c34d268f8278a834139e5478fcf18d.png

i.gyazo.com/d5cefaf2794c56b4408557e548e714fe.png


Read the Thread again... Nvm.
I don't use regions at all. Split them into folders with their corresponding functionality, like a forms folder for forms, helpers for utilities, models for objects, etc.


as well as comments if needed.

[Image: 1zL3pDG.jpg]

Nice contribution, but I'm afraid there is a lot more to organizing your code than just using regions. Using regions is probably the last step in my opinion to organizing code, and it's something that is not available for other IDEs so there must be more universal ways for writing organized code. Here's my tips on organizing code.

1) Use a certain design architecture when creating applications, such as the three tier application model or MVC.
2) Follow and stick to the naming conventions set by Microsoft
3) Make use of folders, group similar files into folders. For example, create a folder called Forms where you put all your forms in.
4) Comment, comment and comment your code.
5) Modular and reuse your code, make great use of object oriented programming, and design classes that are modular.
6) Make use of the single responsibility principals, each class should do one thing and one thing only.
7) Ensure your classes are easily maintainable and scale-able
8) Never ever ever put your business logic code and your data access code in the same class or folder, they should both be seperated.
9) Make use a abstract classes
10) Follow the S.O.L.I.D design principals of OOP design
http://www.codeproject.com/Articles/7036...ple-Csharp
you propably should simply rename the thread to: "How to create regions inside a method in vb.net"
(Jul 3, 2015 12:59 PM)DisguiserNET Wrote:  you propably should simply rename the thread to: "How to create regions inside a method in vb.net"

Yeah, that is a better title. I have now changed it.
Nice Tutorial, Read it like 3 times.

The first 2 times I read this I thought you said that we couldn't make #Regions anywhere :P
If your subs/functions are getting so out of control that you need to add regions within them, then you are doing something wrong.
[Image: vjPWhyy.gif]
(Jul 4, 2015 02:45 AM)$lick Wrote:  If your subs/functions are getting so out of control that you need to add regions within them, then you are doing something wrong.

if you are used to big screens and then have to work on your netbook, regions are quite usefull
#Region is the worst way to organize code, in my opinion, I think it more helps in de-organizing the code.

(Jul 4, 2015 02:45 AM)$lick Wrote:  If your subs/functions are getting so out of control that you need to add regions within them, then you are doing something wrong.

+1 on this.
You should rather split code from your subs/functions into more subs/functions, and use them in the first sub/function which was so huge you needed regions lol, much better organization.
(Jul 4, 2015 09:45 PM)Menalix Wrote:  #Region is the worst way to organize code, in my opinion, I think it more helps in de-organizing the code.

+1 on this.
You should rather split code from your subs/functions into more subs/functions, and use them in the first sub/function which was so huge you needed regions lol, much better organization.

Agreed. I tend to use Regions only when grouping e.g a function set or properties, but never as a substitute of what could be a class or method itself.
(Jul 4, 2015 09:45 PM)Menalix Wrote:  #Region is the worst way to organize code, in my opinion, I think it more helps in de-organizing the code.

+1 on this.
You should rather split code from your subs/functions into more subs/functions, and use them in the first sub/function which was so huge you needed regions lol, much better organization.

I agree with the bottom statement not with the top though. Regions are useful for code organization. I like to separate my functions, declarations, and properties for one. It is definitely helpful in making your code easier to understand.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by z1995zFeb 12, 2016 04:13 PM 5 288 Feb 12, 2016 04:13 PM
Last Post: z1995z
[C] by dilipkumarthotaMay 6, 2011 08:29 PM 2 81 May 6, 2011 08:29 PM
Last Post: dilipkumarthota
[.NET] by You Are FreeJan 8, 2011 04:33 PM 21 281 Jan 8, 2011 04:33 PM
Last Post: You Are Free