Organising Code

AVR

New member
Joined
Jan 5, 2006
Messages
3
Programming Experience
10+
Hi,

I have an application that I have been developing over several months. The problem I have is that it is a very long program and it is now tiring having to scroll through the code or use search to find things.

What I need to know is how to simply have "Include" files that form part of the main program.

I am not sure if I must create a new Module or Class within my project to do this?

I would want to have button events for my main form or "Form1" located in these seperate files.

For example, I would want to place the code below into a separte file which is compiled with my main code(form1)

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Textbox1.Text = "HELLO"

EndSub


Textbox1 is located on form 1.


I would also want global variables to be shared between the main form and the seperate include file.


I tried creating a new class called Class1.vb and used "inherits form1" within it. It compiles OK but the above example does not work.

Any help would be appreciated.


 
Bah! Don't bother with that... use Regions instead, then collapse them.


#Region "Add region description here...."
.
. 'Code goes here....
.
#End Region

I group my code into four sections....
The first is the Windows generated code from the IDE, the one that sets up the form.
Then I add three of my own

#Region "Data Access"
#End Region

#Region "Action Code"
#End Region

#Region "Event Handlers"
#End Region

The Data Access Region is where I put all of my Database Calls in. The Action Code region is where the "meat" of the action is. It's the workhorse section. And the Event Handler region is just that... it's all of the event handlers - which usualy call some func or sub in the Action Code, which uses the Data Access to get the data. You can then collapse the regions not in use - it will display the description you enter, so make sure it descriptive somewhat. You can also create regions in regions in regions.....

-tg
 
If you're using VB 2005 then you can use partial classes, i.e. several files each containing parts of the same class. You simply declare "Public Partial Class SomeClass" in each. Either version supports regions. To be honest, there isn't much reason for a single developer to use partial classes outside of what is done by default, with the auto-generated code in its own file to help prevent it being inadvertently altered. As tg suggests, regions used intelligently should make even a huge file easily navigable.
 
Back
Top