vb.net - database app

I have an app I'm writing that uses the same form for 4 different things.  To add records, view records, delete records, and edit records.  

The code is beginning to become hard to manage.  How do the experts recommend organizing the code?  Modules?  Just not sure what to do here...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

#Region " This is one way "
    just a quick fix that you can do right now to make the code more manageable
#End Region

It doesn't physically organize things for you, but - visually - it rocks.  I maintain an app with abunchof forms, but the users generally just use one form for everything.  it's regions at the moment look something like ...

Private Fields
Private Properties
Public Properties
AddHandler methods

   ... abunchof regions in here ... Form, Click/DoubleClick, Keyboard, Context Menu, SelectedIndexChanged, Mouse Events, Drag Events, Text Events, Enter/Leave, Timer, ErrorHandler, ...

Position Changed Methods
Dataset Update Methods
Datasource Update Methods
Collection Add/Update/Clear methods
Control Add/Update/Clear methods
Search/Filter methods
Conversion Functions
User Management ...

and then a very very big "misc (to organize)" region

i recommend better organizing yours ... but, you're not going to be able to put lots of your code in other classes, etc.  (I never used modules ... .NET makes your modules classes anyway).  I do use a class to store all of my SQL and OLEDB/ADO.NET objects ... which all forms use ... I break as many things off into other/new classes as I can ... but all of your events/lots of methods are going to have to be within the form class, so ... you just need to manage them all as best as you can.

A word of advice re: Events

VB.NET lets you just write Public Sub(sender/e) *Handles* for your WithEvents variables/objects ... i LOVE it for quick and dirty, but i never use it for high-traffic event methods.  I always go thru my controls on form load and addhandlers to, for ex ... I addhandlers to all of my FooBarPicturebox.Click events ... to the addressOf FooBarPictureboxes_Click ... and do checks on the 'sender' ... you can have multiple control.event's in your Public Sub(sender/e) Handles x.click, y.click, z.click ... but sending abunchof things to one or a few methods REALLY keeps the amount of code down.

other folks will get you good advice -- just my $0.02 here.  Also: as often as you can ... use classes / methods so, when the boss says you need to COMPLETELY re-do how something works ... you only have to update code in ONE PLACE and it'll work.  Lots of my objects/controls/collections are compiled to .dll's so i can often just change the code there and re-compile and everything's fixed/changed.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

if you don't know already, you can right click toward the left of the code editor in VS (click where the +/- expansions are) and Outlining > Toggle All Outlining.

if atleast one thing is collapsed, it'll expand everything.  but then, click again (when everything's expanded) and it'll collapse everything (not regions -- but everything else).  if you keep your regions collapsed when you're not workig within them ... it makes the code really easy to navigate when you're going "now where the ... did i i put that function!?"  and you don't want to ctrl-F find, or you can't remember what you named it.
tobin46Author Commented:
Can you talk a little more about compiling to dll's...someone mentione this to me before...
Well -- if you have abunchof controls or other classes that you edit every so often but you want to seperate out a bit from your project so you can use them in other apps or ... just to keep things organized, you can make a Class Library project (in your current solution or in another) that'll compile down to a .dll that you can, then, reference in your project.

So ... if your listbox control is bright purple and your boss says s/he wants it to be blue ... just edit/recompile the .dll (and either recompile your app or, if the assembly version # is the same, you can just copy it into the app's bin -- though you should re-reference the dll and recompile if you've made a lot of changes) ... and you will have only edited the library, and your app should have a newly blue listbox.

or if you have Dog objects in your listbox and their ToString() returns the dog's name and your boss want's "Name (breed)" to show up in the listbox ... go into the library project with the dog class and modify the ToString() ... recompile - overwrite the one in your app's bin ... and voila.

dll's are only sortof helpful with one application (tho i use them regardless to keep things organized).  You'll *really* want to compile to class libraries (dll's) when you want to use the classes in other applications or distribute them.  Maybe you have interface classes and such that let other developers connect to / "talk to" your own application ... compile those dlls and distribute them with/as an API for your application.

... and ... whatever else people compile to dll's for ...

namespaces are always good to help you organize things, as well ... i like to hit the dot after a namespace and see all of my classes/more namespaces well-organized.  mmm, intellisense :P
tobin46Author Commented:
Thanks, you've peaked my interest and I'm going to move in this direction.  Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.