Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Shared Subs?

Reply
Thread Tools

Shared Subs?

 
 
DOUGLAS HEESTAND
Guest
Posts: n/a
 
      03-01-2004
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!
 
Reply With Quote
 
 
 
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      03-02-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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!



 
Reply With Quote
 
 
 
 
Douglas Heestand
Guest
Posts: n/a
 
      03-02-2004
Alvin Bruney [MVP] wrote:
> Your post went unanswered. Have you resolved this issue?
>

Not yet. thanks!
 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      03-02-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Alvin Bruney [MVP] wrote:
> > Your post went unanswered. Have you resolved this issue?
> >

> Not yet. thanks!



 
Reply With Quote
 
Douglas Heestand
Guest
Posts: n/a
 
      03-03-2004
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!
 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      03-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!



 
Reply With Quote
 
Douglas Heestand
Guest
Posts: n/a
 
      03-03-2004
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") = "(E-Mail Removed)"
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!
 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      03-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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") = "(E-Mail Removed)"
> 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!



 
Reply With Quote
 
Douglas Heestand
Guest
Posts: n/a
 
      03-03-2004
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") = "(E-Mail Removed)"
End Sub


</script>



Thanks so much.


 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      03-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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") = "(E-Mail Removed)"
> End Sub
>
>
> </script>
>
>
>
> Thanks so much.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Global Shared functions, shared variables Imran Aziz ASP .Net 1 08-22-2005 06:43 PM
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
Shared, why not a 'Local Shared' (re: Session and ViewState dislike) ben ASP .Net 3 11-15-2004 03:04 PM
Shared Public Variables and Shared Methods Joe Fallon ASP .Net 3 07-16-2004 07:11 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments