reusing a bit of code?

Discussion in 'ASP .Net' started by Rob Meade, Jan 30, 2004.

  1. Rob Meade

    Rob Meade Guest

    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
     
    Rob Meade, Jan 30, 2004
    #1
    1. Advertising

  2. 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 Meade schrieb:

    > 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
    >
    >
     
    Steffen Loringer, Jan 30, 2004
    #2
    1. Advertising

  3. Rob Meade

    Janaka Guest

    Rob

    Sounds like your a bit new to the concepts of Object Orientated Programming.
    If your using Subs its probably VB.NET your programming in. Have a look at
    the following article to get a hang of some of the concepts:
    http://www.ondotnet.com/pub/a/dotnet/2001/09/25/oop_vb.html

    J

    "Rob Meade" <> wrote in message
    news:%...
    > 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
    >
    >
     
    Janaka, Jan 30, 2004
    #3
  4. 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
    "Rob Meade" <> wrote in message
    news:%...
    > 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
    >
    >
     
    Marshal Antony, Jan 30, 2004
    #4
  5. 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.

    "Rob Meade" <> wrote in message
    news:#...
    > 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
    >
    >
     
    Kevin Spencer, Jan 30, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. - Steve -

    Reusing Code Snippets

    - Steve -, Sep 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    427
    Greg Burns
    Oct 1, 2004
  2. Zodiac

    Reusing Code

    Zodiac, Feb 9, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    422
    Kevin Spencer
    Feb 9, 2005
  3. Replies:
    3
    Views:
    1,764
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    980
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    179
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page