Organize Code

D

Daniel N

I am new to .net and want to organize my code better. I am writing in vb.net
and the code for my main form is nearing 50-60 pages and would like to
create another file like a class, module or code file that SHARE sub
procedures, and declarations as if it were with the rest of the code (So I
can orginize it in 2 or 3 .vb files).



When I create a new class I can;

Inherits Project.MainForm



But I cant share subs,



Can I somehow do this with a code file or module?
 
V

V

Hello Daniel,

If its just code-organization that you are looking at and if you are
using 2.0 version of Dot Net, then you may want to read up on Partial
Classes. This feature allows you to break up one class into many files,
thus helping the organization of the code better. I typically have a
separate file for public methods, a separate one for properties, and a
separate one for private methods and member variables.

Also, if your classes are getting SO bog, then you may want to refactor
by making new classes.

- Vaibhav
 
C

Chris Mullins

Daniel N said:
I am new to .net and want to organize my code better. I am writing in
vb.net and the code for my main form is nearing 50-60 pages [...]

It's time for some software engineering. Stop slinging code, take a step
back, and design your solution.

You need business entities of some sort, be they factories, class entities,
or typed datasets. Seperate your presentation tier from your business logic,
and from your data tier.

This isn't a language or a coding issue, but a design issue. There's no
language feature that going to solve your problem, not partial classes, not
generics, not anonymous methods. Fire up Visio, and start doing some UML
diagrams...
 
C

Cor Ligthert [MVP]

Daniel,

VB is OOP now, that means exactly that you don't share things as it was done
in modulair programming.

You create Objects as you need them. Therefore you make classes (templates)

If you need such an object you can say.

Dim myObject as New MyClass

You can use that myObject now in your method.

(I reallise me, that this is a very short way of telling it).

A to this dedicated newsgroup in VB is the newsgroup
microsoft.public.dotnet.languages.vb

I hope this gives a starting idea,

Cor
 
G

Guest

I am new to .net and want to organize my code better. I am writing in vb.net
and the code for my main form is nearing 50-60 pages and would like to
create another file like a class, module or code file that SHARE sub
procedures, and declarations as if it were with the rest of the code (So I
can orginize it in 2 or 3 .vb files).
...
Can I somehow do this with a code file or module?

Create a new Module file file in your project and add code like below:

Public Module WhateverModule
Public Sub Abcde()
End Sub
End Module

Sub Abcde will be visible (callable) from your form. You can cut/paste subs
and functions from your form vb file to this vb file. If you make them
public, they will be callable from your form.

There are two things that will impede moving a sub into the module. First,
you may have variables declared in your form that are used by a sub, and such
subs are poor candidates for moving to the module. Second, the sub may
reference form properties (eg Show, Hide, etc) in which case you would need
to augment the sub something like this
private sub xxxx(...)
becomes
public sub xxxx(byval MyForm as form,...)
In xxxx, you will have to change Show to MyForm.Show.

The above suggestion directly answers your question, but it is not an OO
suggestion. Other responders have suggested rethinking your program
organization, and I agree. Whether you rethink or not, the technique you
will follow is to create new module and/or class files, cut and paste from
the form into the new file, and tweak the code to make it work. In some
cases, the tweaking will be substantial.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top