reusing a bit of code?

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

I'm in the middle of finishing a page and I've noticed that I have a chunk
of code that is used in two places - its exactly the same - I guess I can
rip this out - save it seperately and then call it into both areas when
needed - is this a 'class' - I'm not fully up with all the terminology...

So what should I do - within the same page create a sub and put it in there
then call it - or save it to a different type of file (somone indicate which
one?) and then reference it somehow to use it?

Any help would be appreciated,

Regards

Rob
 
The easierst way is to use an additional sub in your aspx file. But it's
necessary to identify all variables needed for the sub.
If there are some local variables that are needed for the sub it's
helpful to use Sub MySub(ByRef A As Interger) .... when calling the sub.

HTH
Steffen
 
Rob,
I would advice to make commonly used code in a dynamic-link
library(dll)(may be more than one depends on your needs)
using .NET and reference it in your pages.The advantage is that you can
reuse it for many applications.
The dll can contain multiple classes each has certain functions,procedures
etc. under a namespace.
Understand assemblies and namespaces to learn more on this.
Here is a link :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/assenamesp.asp

Hope this helps,
Marshal Antony
http://www.dotnetmarshal.com
For Eg
 
Hi Rob,

It's good that you are noticing repeated code, and thinking that you should
make it available in one location for optimal performance and
maintainability of your app. That is just good sense, and is, in fact, one
of the principal considerations that resulted in Object-oriented programming
concepts.

In procedural programming, you use Subs and Functions in Function libraries
(or DLLS) for this purpose. Object-Oriented programming extends the idea of
re-usable code with the idea of reusable classes. The classes contain data
as well as Functions and Subs (which are called "Methods" in OOP). A class
is basically a structure containing both data (n the form of fields and
properties) and executable code in the form of Functions and Subs (Methods).
A field is like a global variable in that it is simply a variable with the
additional attribute of accessibility (encapsulation). Accessibility is how
accessible the variable is. Public fields/properties/methods are accessible
to anything. Private fields/properties/methods are accessible only within
the given class. Protected are accessible by any class which inherits the
given class. Etc. Properties are actually Methods, but appear much like
Fields in terms of accessing them. However, since the Property has a get and
set method, other processing can be done when getting or setting the value
of the Property.

You really should read up on OOP principles at some point to become more
familiar with how to use them. In the meantime, however, I would advise you
to create a class library (project) of functions (Methods) that you want to
use in multiple pages.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top