Best place for "Global Variables" and Methods in C# though ASP.Net

Discussion in 'ASP .Net' started by Bub.Paulson@gmail.com, Oct 17, 2007.

  1. Guest

    A month ago I finally took the plunge and began learning C# and
    ASP.Net, coming from a Classic ASP and VBScript background.

    In my classic ASP, I had my own little library of code that I stuck in
    an include file called "Common_VBscript.asp" which had all of the
    common stuff I used. (Constants, Connection Strings, little utlities
    like "email validation" functions, or functions to ready strings for
    db entry, etc.)

    What is the best way for me to have a "common code" module tacked on
    to all (or many) of my ASP.Net pages for a particular project? And
    how can I do it, precisely?

    Thanks,
    B.P.
    , Oct 17, 2007
    #1
    1. Advertising

  2. <> wrote in message
    news:...

    >A month ago I finally took the plunge and began learning C# and
    > ASP.Net, coming from a Classic ASP and VBScript background.
    >
    > In my classic ASP, I had my own little library of code that I stuck in
    > an include file called "Common_VBscript.asp" which had all of the
    > common stuff I used. (Constants, Connection Strings, little utlities
    > like "email validation" functions, or functions to ready strings for
    > db entry, etc.)
    >
    > What is the best way for me to have a "common code" module tacked on
    > to all (or many) of my ASP.Net pages for a particular project? And
    > how can I do it, precisely?


    ASP.NET, like the rest of the .NET Framework, is object-orientated.
    Generally speaking, if you're looking for "global" anything, you probably
    need a bit of a refresher on OOP...

    An obvious solution is to create a new class with common functions. Then,
    whenever you need one or more of them, just instantiate the class and use
    the function(s) you need...

    In addition, C# supports classes with static methods which you can use
    anywhere in your code without instantiating the class. Purists may flinch at
    doing this, but it can be useful:
    http://www.eggheadcafe.com/articles/20020206.asp

    And, purely in the interests of balance, VB.NET can do the same thing with
    its Shared methods...

    However, be very wary of static variables, as they will be common across all
    sessions, which can lead to some very unexpected and unwanted results...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 17, 2007
    #2
    1. Advertising

  3. IfThenElse Guest

    in addition use the web.config for encrypted connection string and other
    configurations, may additionally use XML config files etc..

    may want to have some aspx pages inherit from some base with common
    functionality.

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > <> wrote in message
    > news:...
    >
    >>A month ago I finally took the plunge and began learning C# and
    >> ASP.Net, coming from a Classic ASP and VBScript background.
    >>
    >> In my classic ASP, I had my own little library of code that I stuck in
    >> an include file called "Common_VBscript.asp" which had all of the
    >> common stuff I used. (Constants, Connection Strings, little utlities
    >> like "email validation" functions, or functions to ready strings for
    >> db entry, etc.)
    >>
    >> What is the best way for me to have a "common code" module tacked on
    >> to all (or many) of my ASP.Net pages for a particular project? And
    >> how can I do it, precisely?

    >
    > ASP.NET, like the rest of the .NET Framework, is object-orientated.
    > Generally speaking, if you're looking for "global" anything, you probably
    > need a bit of a refresher on OOP...
    >
    > An obvious solution is to create a new class with common functions. Then,
    > whenever you need one or more of them, just instantiate the class and use
    > the function(s) you need...
    >
    > In addition, C# supports classes with static methods which you can use
    > anywhere in your code without instantiating the class. Purists may flinch
    > at doing this, but it can be useful:
    > http://www.eggheadcafe.com/articles/20020206.asp
    >
    > And, purely in the interests of balance, VB.NET can do the same thing with
    > its Shared methods...
    >
    > However, be very wary of static variables, as they will be common across
    > all sessions, which can lead to some very unexpected and unwanted
    > results...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    IfThenElse, Oct 17, 2007
    #3
  4. Guest

    Thank you for the reply! A few questions based on your comments
    follow:

    > An obvious solution is to create a new class with common functions. Then,
    > whenever you need one or more of them, just instantiate the class and use
    > the function(s) you need...


    Yes, that sounds terrific, and is what I had in mind. Trouble is,
    while I understand how to make "Web Forms" (ASPX files) and "Web User
    Controls" (ASCX files), making a separate class and being able to
    include it only in needed pages escapes me. In Visual Studio, I can
    see "make class" when adding a new item, but it wants to stick it into
    "App_Code", which I do not think is where I want it. (This will make
    whatever static variables I use common across the entire application,
    right? There are many times where I am using variables I only want
    "common" to particular sessions.)

    Can someone give me explicit directions on how to include a Class that
    I have made (a file called "Common.cs) for instance in specific pages
    that use methods and variables that it includes, without using
    "App_Code", or _SHOULD_ I be using "App_Code" for this?

    Thanks,
    B.Paulson
    , Oct 17, 2007
    #4
  5. IfThenElse Guest

    do not use static then if it is not common to all. or have a mix of static
    that is common to all and Instance variables, members, or Methods.

    create your new class, and it does not matter where VS.NET put it leave it
    where it wants it to be.

    then Instantiate your class where you want to use it. ( if in the same
    namespace no need to add a reference )

    Dim myClass as Common = new Common()

    You can have properties or Methods in your common class.

    you can hydrate your new object myClass with session-data via properties or
    constructor.

    you can call methods on the object myClass as you wish.

    for static members you don't need to instantiate.



    <> wrote in message
    news:...
    > Thank you for the reply! A few questions based on your comments
    > follow:
    >
    >> An obvious solution is to create a new class with common functions. Then,
    >> whenever you need one or more of them, just instantiate the class and use
    >> the function(s) you need...

    >
    > Yes, that sounds terrific, and is what I had in mind. Trouble is,
    > while I understand how to make "Web Forms" (ASPX files) and "Web User
    > Controls" (ASCX files), making a separate class and being able to
    > include it only in needed pages escapes me. In Visual Studio, I can
    > see "make class" when adding a new item, but it wants to stick it into
    > "App_Code", which I do not think is where I want it. (This will make
    > whatever static variables I use common across the entire application,
    > right? There are many times where I am using variables I only want
    > "common" to particular sessions.)
    >
    > Can someone give me explicit directions on how to include a Class that
    > I have made (a file called "Common.cs) for instance in specific pages
    > that use methods and variables that it includes, without using
    > "App_Code", or _SHOULD_ I be using "App_Code" for this?
    >
    > Thanks,
    > B.Paulson
    >
    IfThenElse, Oct 17, 2007
    #5
  6. "IfThenElse" <> wrote in message
    news:...

    >> I have made (a file called "Common.cs) for instance in specific pages

    >
    > Dim myClass as Common = new Common()


    He's using C#...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 17, 2007
    #6
  7. IfThenElse Guest

    Sorry,


    Common myClass = new myClass(...);


    "Mark Rae [MVP]" <> wrote in message
    news:u2jdu$...
    > "IfThenElse" <> wrote in message
    > news:...
    >
    >>> I have made (a file called "Common.cs) for instance in specific pages

    >>
    >> Dim myClass as Common = new Common()

    >
    > He's using C#...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    IfThenElse, Oct 18, 2007
    #7
  8. IfThenElse Guest

    Sorry,

    Common myClass = new Common(...);


    "Mark Rae [MVP]" <> wrote in message
    news:u2jdu$...
    > "IfThenElse" <> wrote in message
    > news:...
    >
    >>> I have made (a file called "Common.cs) for instance in specific pages

    >>
    >> Dim myClass as Common = new Common()

    >
    > He's using C#...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    IfThenElse, Oct 18, 2007
    #8
  9. kaza Guest

    On Oct 17, 5:09 pm, wrote:
    > A month ago I finally took the plunge and began learning C# and
    > ASP.Net, coming from a Classic ASP and VBScript background.
    >
    > In my classic ASP, I had my own little library of code that I stuck in
    > an include file called "Common_VBscript.asp" which had all of the
    > common stuff I used. (Constants, Connection Strings, little utlities
    > like "email validation" functions, or functions to ready strings for
    > db entry, etc.)
    >
    > What is the best way for me to have a "common code" module tacked on
    > to all (or many) of my ASP.Net pages for a particular project? And
    > how can I do it, precisely?
    >
    > Thanks,
    > B.P.


    for this thing I alway use Static Classes, and they never have never
    member variables as problems will occure as mentioned in other thread
    when sharing data this way
    kaza, Oct 18, 2007
    #9
  10. Guest

    Thanks for the help, folks. (Esp. IfThenElse, ;))

    I will give it a whirl and report back.

    Thanks,
    B.P.
    , Oct 19, 2007
    #10
  11. Guest

    On Oct 19, 1:58 pm, wrote:
    > Thanks for the help, folks. (Esp. IfThenElse, ;))
    >
    > I will give it a whirl and report back.
    >
    > Thanks,
    > B.P.


    A couple weeks later and I have learned A LOT!

    The responses here were great, but this page from a person with a
    similar background really drove things home:

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=17

    So... I thought I would give this thread some "closure" by including a
    useful link in case someone ever reads this thread seeking answers...

    B.P.
    , Nov 8, 2007
    #11
    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. Wayne
    Replies:
    2
    Views:
    459
    Wayne
    Nov 11, 2003
  2. Alan Silver
    Replies:
    9
    Views:
    385
    Alec MacLean
    Apr 21, 2004
  3. craig dicker
    Replies:
    9
    Views:
    664
    Juan T. Llibre
    Jul 7, 2005
  4. Replies:
    3
    Views:
    400
    =?Utf-8?B?SmFnZGlzaCBZYWRhdg==?=
    Jun 21, 2006
  5. Error Hosting ASP.NET 2.0 though UNC

    , Jun 6, 2007, in forum: ASP .Net Security
    Replies:
    0
    Views:
    107
Loading...

Share This Page