Adding addtional Modules to a C# Solution.

Mr_Fulano
Mr_Fulano used Ask the Experts™
on
Hi, I'm using MS VS2010, C#.NET. I currently have an application with a lot of methods all rolled up above the code for my Controls. I'd like to organize my code-behind in a more intuitive manner (at least intuitive for me) and be able to add Modules (additional Tabs) where I can put all the code that is general math related into one Tab and another Tab for all the code dealing with hexadecimal manipulations, etc. I guess I can use Namespaces, but is that the best approach. I recall in VB.NET I was able to add Modules to the application and have different modules (i.e. pages , Tabs) for different code types.

Is that possible in C#, if so how would I do that, and if not what is a better approach other than to have all my code on one page all rolled up?

Thanks,
Fulano
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Modules get compiled into Shared classes. The equivalent in C# would be a static class--C# does not have a concept of "modules" like VB does. Depending on your design, this may or may not be beneficial for you.

static classes are generally used for utility methods which retain no state. Take the Math class for example. It is a static class, and it contains numerous static methods which have no need for state. Take Math.Abs for example. To calculate the absolute value a number, is there anything you need to maintain between multiple calls to the method? Does this method not produce the same exact value for multiple calls if you pass the same parameter multiple times? This is an example of such a utility usage.

There are various viewpoints on the use of static classes. Some people say they are fine for utility methods, yet others say that they are not true OO constructs--which they are not. As I mentioned above, it would depend on your design or preference as to how you use them.

The short answer to your question is that you are going to need new classes. Whether or not you make them static is a decision you will have to make.
Michael FowlerSolutions Consultant

Commented:
If you are trying to make a class easier to read use regions eg
#region Math Code

//Methods related to Maths here

#endregion

Open in new window


Visual Studion allows to collapse regions and so makes it far easier to navigate your code to find what you are after.

Michael

Author

Commented:
Hi Michael74, yes...by "rolled up" in my original post, I mean I have them all in regions, but its still messy! I was hoping to spread things out a bit and have designated areas for certain types of code.

Thanks,
Fulano
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
Hi Kaufmed, good suggestions. I'll give that approach a try and see where I go with it.

Thanks,
Fulano
anarki_jimbelSenior Developer

Commented:
I don't like modules. Really, they exist just for backward compatibility with VB6 and prior.

I a class becomes too large - try to re-factor to separate classes. Check that no business logic in a presentation layer - move it to business classes.

Idea with regions is also good - I use it all the time.
Michael FowlerSolutions Consultant

Commented:
I personally use a combination. As noted above keeping presentation and business logic seperated is always a good idea but I like to use regions to seperate code into sections eg

Public Varaibles
Private Variables
Constructors
Button Click Events
etc..

Fulano
If you are finding that some regions are getting out of hand then look at seperating these regions out into new classes. Static classes are very handy for this.

Michael

Author

Commented:
Hi Michael, regions is what I use now and its a bit too messy for my taste. Regions work for small areas of code, but when you begin to have 4 or 5 different methods within that region, then its too hard to keep under control.

I was thinking of Classes, but I guess the problem I'm struggling with is that a Class can be instantiated into an Object, so I'm thinking my MathClass can be instantiated into a MathClass Object, which wouldn't make any sense given that its a Class without a constructor (well...all Classes have a default constructor that really doesn't do anything), and that Class is full of methods.

Is that a good idea....?

Thanks,
Fulano
Solutions Consultant
Commented:
This is where static classes are good. Static classes cannot be instantiated. You simply call the methods in these classes using the format
<class>.<method>

Here is a link with more info
http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

Michael

Author

Commented:
Excellent....this is what I needed to understand. -- Static Classes is what will help me refactor my code. Thank you both!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial