Shared Subs?

Discussion in 'ASP .Net Web Controls' started by DOUGLAS HEESTAND, Mar 1, 2004.

  1. How do you create a library of shared subs to be used by all your
    pages? I thought I would create a public class called "Global" with a
    bunch of public subs, but how do I instantiate this new global class
    from other pages? How does ASP.NET know where the source code for the
    class is?

    Thanks!
    DOUGLAS HEESTAND, Mar 1, 2004
    #1
    1. Advertising

  2. Your post went unanswered. Have you resolved this issue?

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "DOUGLAS HEESTAND" <> wrote in message
    news:...
    > How do you create a library of shared subs to be used by all your
    > pages? I thought I would create a public class called "Global" with a
    > bunch of public subs, but how do I instantiate this new global class
    > from other pages? How does ASP.NET know where the source code for the
    > class is?
    >
    > Thanks!
    Alvin Bruney [MVP], Mar 2, 2004
    #2
    1. Advertising

  3. Alvin Bruney [MVP] wrote:
    > Your post went unanswered. Have you resolved this issue?
    >

    Not yet. thanks!
    Douglas Heestand, Mar 2, 2004
    #3
  4. Can you repost the original question, I seemed to have lost it. I can take a
    look at it if you repost.

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Douglas Heestand" <> wrote in message
    news:...
    > Alvin Bruney [MVP] wrote:
    > > Your post went unanswered. Have you resolved this issue?
    > >

    > Not yet. thanks!
    Alvin Bruney [MVP], Mar 2, 2004
    #4
  5. Alvin Bruney [MVP] wrote:

    > Can you repost the original question, I seemed to have lost it. I can take a
    > look at it if you repost.
    >

    Sure, thanks for taking the time to help me. Here it is:

    How do you create a library of shared subs to be used by all your
    pages? I thought I would create a public class called "Global" with a
    bunch of public subs, but how do I instantiate this new global class
    from other pages? How does ASP.NET know where the source code for the
    class is? It seems like this should be a pretty common thing, but I
    can't figure it out.

    Thanks!
    Douglas Heestand, Mar 3, 2004
    #5
  6. Declare a small class in your global.asax file. Provide static functions
    like so

    Public static myClass
    {
    private static void readMe();
    }

    or simply add the required functions to the already existing global class.

    in your default webform1.aspx.* file
    you can call it like so

    myClass.readMe();

    hth

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Douglas Heestand" <> wrote in message
    news:...
    > Alvin Bruney [MVP] wrote:
    >
    > > Can you repost the original question, I seemed to have lost it. I can

    take a
    > > look at it if you repost.
    > >

    > Sure, thanks for taking the time to help me. Here it is:
    >
    > How do you create a library of shared subs to be used by all your
    > pages? I thought I would create a public class called "Global" with a
    > bunch of public subs, but how do I instantiate this new global class
    > from other pages? How does ASP.NET know where the source code for the
    > class is? It seems like this should be a pretty common thing, but I
    > can't figure it out.
    >
    > Thanks!
    Alvin Bruney [MVP], Mar 3, 2004
    #6
  7. Thanks for your help. I have been down this road before and I got
    stuck. When I try to put a class in my global.asax file it gives a
    compilation error (It highlights the "Public Class myClass" line and
    says "Keyword is not valid as an identifier"). My global.asax file is
    below (using VB):

    <script language="VB" runat="server">

    Sub Application_Start(Sender As Object, E As EventArgs)
    'Declare Application-wide Constants
    Application("EMAIL_ADDRESS") = ""
    End Sub

    Public Class myClass
    Public Shared Sub SendCustomerEmail(strEmail as String, strUserInfo as
    String, strRegInfo as String)
    Dim msg as new MailMessage
    msg.From = Application("EMAIL_ADDRESS")
    msg.To = strEmail
    msg.Subject = "Subject"
    msg.Body = "Thank you!"
    msg.Body &= strUserInfo & "<br>" & strRegInfo
    msg.BodyFormat = MailFormat.Html
    SmtpMail.SmtpServer = "localhost"
    SmtpMail.Send(msg)
    End Sub
    End Class

    </script>

    SHould I be using some other syntax?

    Thanks!
    Douglas Heestand, Mar 3, 2004
    #7
  8. Your class definition needs to go OUTSIDE the first public class declaration
    in the file. You currently have it inside the class declaration
    namespace blah
    declare your class here
    other normal class here

    what you have is this
    namespace blah
    other normal class here
    your class here

    makes sense?

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Douglas Heestand" <> wrote in message
    news:...
    > Thanks for your help. I have been down this road before and I got
    > stuck. When I try to put a class in my global.asax file it gives a
    > compilation error (It highlights the "Public Class myClass" line and
    > says "Keyword is not valid as an identifier"). My global.asax file is
    > below (using VB):
    >
    > <script language="VB" runat="server">
    >
    > Sub Application_Start(Sender As Object, E As EventArgs)
    > 'Declare Application-wide Constants
    > Application("EMAIL_ADDRESS") = ""
    > End Sub
    >
    > Public Class myClass
    > Public Shared Sub SendCustomerEmail(strEmail as String, strUserInfo as
    > String, strRegInfo as String)
    > Dim msg as new MailMessage
    > msg.From = Application("EMAIL_ADDRESS")
    > msg.To = strEmail
    > msg.Subject = "Subject"
    > msg.Body = "Thank you!"
    > msg.Body &= strUserInfo & "<br>" & strRegInfo
    > msg.BodyFormat = MailFormat.Html
    > SmtpMail.SmtpServer = "localhost"
    > SmtpMail.Send(msg)
    > End Sub
    > End Class
    >
    > </script>
    >
    > SHould I be using some other syntax?
    >
    > Thanks!
    Alvin Bruney [MVP], Mar 3, 2004
    #8
  9. I think we are making progress and I really appreciate your help. When
    I move my public class to the top of the global.asax file it starts
    having other compilation troubles. First it highlights "Dim msg as new
    MailMessage" and says "Type 'MailMessage' is not defined." So I realize
    I need to add a "Imports System.Web.Mail" statement. No matter where I
    put that statement it throws an error, either "'Imports' statements must
    precede any declarations." or if I put it as the first line then "The
    content in the application file is not valid."

    Any ideas?

    My new global.asax file:
    ---------------------------------
    <script language="VB" runat="server">
    Imports System.Web.Mail

    Public Class myClass
    Public Shared Sub SendCustomerEmail(strEmail as String,
    strUserInfo as String, strRegInfo as String)
    Dim msg as new MailMessage
    msg.From = Application("EMAIL_ADDRESS")
    msg.To = strEmail
    msg.Subject = "Subject"
    msg.Body = "Thank you!"
    msg.Body &= strUserInfo & "<br>" & strRegInfo
    msg.BodyFormat = MailFormat.Html
    SmtpMail.SmtpServer = "localhost"
    SmtpMail.Send(msg)
    End Sub
    End Class

    Sub Application_Start(Sender As Object, E As EventArgs)
    'Declare Application-wide Constants
    Application("EMAIL_ADDRESS") = ""
    End Sub


    </script>



    Thanks so much.
    Douglas Heestand, Mar 3, 2004
    #9
  10. just fully qualify it as
    System.Web.Mail.MailMessage

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Douglas Heestand" <> wrote in message
    news:...
    > I think we are making progress and I really appreciate your help. When
    > I move my public class to the top of the global.asax file it starts
    > having other compilation troubles. First it highlights "Dim msg as new
    > MailMessage" and says "Type 'MailMessage' is not defined." So I realize
    > I need to add a "Imports System.Web.Mail" statement. No matter where I
    > put that statement it throws an error, either "'Imports' statements must
    > precede any declarations." or if I put it as the first line then "The
    > content in the application file is not valid."
    >
    > Any ideas?
    >
    > My new global.asax file:
    > ---------------------------------
    > <script language="VB" runat="server">
    > Imports System.Web.Mail
    >
    > Public Class myClass
    > Public Shared Sub SendCustomerEmail(strEmail as String,
    > strUserInfo as String, strRegInfo as String)
    > Dim msg as new MailMessage
    > msg.From = Application("EMAIL_ADDRESS")
    > msg.To = strEmail
    > msg.Subject = "Subject"
    > msg.Body = "Thank you!"
    > msg.Body &= strUserInfo & "<br>" & strRegInfo
    > msg.BodyFormat = MailFormat.Html
    > SmtpMail.SmtpServer = "localhost"
    > SmtpMail.Send(msg)
    > End Sub
    > End Class
    >
    > Sub Application_Start(Sender As Object, E As EventArgs)
    > 'Declare Application-wide Constants
    > Application("EMAIL_ADDRESS") = ""
    > End Sub
    >
    >
    > </script>
    >
    >
    >
    > Thanks so much.
    >
    >
    Alvin Bruney [MVP], Mar 3, 2004
    #10
  11. I know I'm a pain the ass. Now it compiles properly (thanks!) but when
    I try to call the sub from another page it says "Name 'myClass' is not
    declared."

    I called it using the following syntax:

    Private Sub btnContinue_Click(Sender As Object, E As EventArgs)
    blah
    myClass.SendCustomerEmail(strEmail, strUserInfo, strRegInfo)
    blah
    End Sub

    So even though we have a Public class defined in the global.asax file it
    still can't find the class. ugh!

    -Doug
    Douglas Heestand, Mar 3, 2004
    #11
  12. you need to qualify it with the global class name. for example the myclass
    was placed inside a class right, say global, by default so your call would
    be

    Global.myClass.SendCustomerEmail.

    > I know I'm a pain the ass.

    the newsgroup community never takes this position. all of us at one point in
    time or another was at your stage and got valuable help from patient
    professionals which helped us grow. fire on. if i can't answer your
    questions, there is always some else who can.

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Douglas Heestand" <> wrote in message
    news:...
    > I know I'm a pain the ass. Now it compiles properly (thanks!) but when
    > I try to call the sub from another page it says "Name 'myClass' is not
    > declared."
    >
    > I called it using the following syntax:
    >
    > Private Sub btnContinue_Click(Sender As Object, E As EventArgs)
    > blah
    > myClass.SendCustomerEmail(strEmail, strUserInfo, strRegInfo)
    > blah
    > End Sub
    >
    > So even though we have a Public class defined in the global.asax file it
    > still can't find the class. ugh!
    >
    > -Doug
    Alvin Bruney [MVP], Mar 4, 2004
    #12
  13. I figured it out. I needed to compile my .vb files manually using the
    vbc command utility. I am developing using DreamWeaver so my .vb
    files are not automatically compiled to dll's.

    thanks so much for all the help!

    -doug
    DOUGLAS HEESTAND, Mar 4, 2004
    #13
  14. DOUGLAS HEESTAND

    Jim Hughes Guest

    Add a src="mycode.vb" attribute to the @Page directive and the web server
    will automatically compile your code when the page is called.

    "DOUGLAS HEESTAND" <> wrote in message
    news:...
    > I figured it out. I needed to compile my .vb files manually using the
    > vbc command utility. I am developing using DreamWeaver so my .vb
    > files are not automatically compiled to dll's.
    >
    > thanks so much for all the help!
    >
    > -doug
    Jim Hughes, Mar 4, 2004
    #14
    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. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    547
    Ben Holness
    Oct 8, 2003
  2. Bolin

    Subs and lists

    Bolin, Nov 2, 2003, in forum: Perl
    Replies:
    6
    Views:
    554
  3. nakhi
    Replies:
    2
    Views:
    385
    nakhi
    Aug 22, 2003
  4. Kevin Spencer

    Re: Shared (Static) Function and Subs

    Kevin Spencer, Mar 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    356
    Carlos Campos
    Mar 3, 2004
  5. Andrew
    Replies:
    1
    Views:
    156
    Brian McCauley
    Jul 13, 2005
Loading...

Share This Page