Hack Forums
[.NET] How To Create Regions In Methods - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] How To Create Regions In Methods (/showthread.php?tid=4891561)

Pages: Pages 1 2


How To Create Regions In Methods - Earn - Jul 3, 2015

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.



* - nathan72389 - Jul 3, 2015

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


* - Earn - Jul 3, 2015

(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]


* - DBots - Jul 3, 2015

Wow. Learned something new. Thanks!


* - VmX5 - Jul 3, 2015

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.


* - SIM-1676753 - Jul 3, 2015

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.


* - +Haze - Jul 3, 2015

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/703634/SOLID-architecture-principles-using-simple-Csharp


* - DisguiserNET - Jul 3, 2015

you propably should simply rename the thread to: "How to create regions inside a method in vb.net"


* - Earn - Jul 3, 2015

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


* - VmX5 - Jul 3, 2015

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


* - $lick - Jul 4, 2015

If your subs/functions are getting so out of control that you need to add regions within them, then you are doing something wrong.


* - DisguiserNET - Jul 4, 2015

(Jul 4, 2015 02:45 AM)Lazy. 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


* - Menalix - Jul 4, 2015

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


* - Odus - Jul 5, 2015

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


* - Vysse - Jul 5, 2015

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