Shared Subs?

  • Thread starter DOUGLAS HEESTAND
  • Start date
D

DOUGLAS HEESTAND

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!
 
A

Alvin Bruney [MVP]

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

Douglas Heestand

Alvin said:
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!
 
A

Alvin Bruney [MVP]

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
 
D

Douglas Heestand

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 address 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!
 
A

Alvin Bruney [MVP]

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?
 
D

Douglas Heestand

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 address removed)"
End Sub


</script>



Thanks so much.
 
D

Douglas Heestand

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
 
A

Alvin Bruney [MVP]

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.
 
D

DOUGLAS HEESTAND

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
 
J

Jim Hughes

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top