Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Shared functions vs Non-Shared Functions

Reply
Thread Tools

Shared functions vs Non-Shared Functions

 
 
Karl Seguin
Guest
Posts: n/a
 
      05-27-2005
Ok...
Your codebehind file is a class which inherits Page.

Page exposes the request/response/server which is why it just works as is..


Your email class isn't "web aware" since it doesn't inherit from Page or any
other "web aware" classes.

To have webawarness you use System.Web.HttpContext.Current. You cannot
import this because it's a property...you simply can't import properties in
vb.net.

youshould be doing:

Imports System.Web

and in your code use
HttpContext.Current.Request

or, as I showed earlier, use
dim context as HttpContext = HttpContext.Current
dim request as HttpRequest = context.Request


We've covered a lot of ground in this thread

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"tshad" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
> wrote in message news:(E-Mail Removed)...
>> You've lost me a bit with the mass amounts of code.
>>
>> That's exactly what I'm saying about moving it around (as a parameter).
>>
>> As for your error. Objects which you typically program with in your page
>> and user controls such as Request, Response, Server, ... are there
>> because they are exposed as part of the System.Web.UI.Control class your
>> page/user control inherits from (couple levels deep).
>>
>> Your email class inherits directly from Object, so Request, Response,
>> Server are meaningless. To get references to these objects within your
>> class functions, you need to use System.Web.HttpContext.Current which
>> returns the current context (ie the web request) which exposes the
>> Response, Request, Server, ...
>>
>> so you would do:
>>
>> dim context as HttpContext = HttpContext.Current
>> if context is nothing then 'possible if someone is trying to use this
>> class outside of a web-scope
>> throw new InvalidOperationException("MyFunc needs to be called from web
>> request") 'maybe you can do something other than throw an exception?
>> end if
>> 'now you have access to your request and response objects, here's an
>> example
>> dim request as HttpRequest = context.Request
>> dim someValue as string =Request.QueryString("blah")
>>

>
> I may be a little dense here.
>
> If I do this:
>
> ************************************************** ****************************
> Imports System.Web.HTTPContext
> Imports Microsoft.VisualBasic
>
> NameSpace MyFunctions
>
> Public Class Email
>
> Public Shared sub sendEmail ( )
> dim URLPath As String = Left(Current.request.path,
> InStrRev(Current.request.path, "/") - 1)
> end sub
>
> End Class
>
> End Namespace
> ************************************************** ****************************
>
> Where I use "Current.request.path" - it works fine.
>
>
> If I do this:
> ************************************************** ****************************
> Imports System.Web.HTTPContext
> Imports System.Web.HTTPContext.Current
> Imports Microsoft.VisualBasic
>
> NameSpace MyFunctions
>
> Public Class Email
>
> Public Shared sub sendEmail ( )
> dim URLPath As String = Left(request.path, InStrRev(request.path, "/") -
> 1)
> end sub
>
> End Class
>
> End Namespace
> ************************************************** ****************************
> Where I have "Imports System.Web.HTTPContext.Current", I get 2 errors:
>
> C:\Inetpub\wwwroot\staffingworkshop\emailold3.vb(2 ) : error BC30466:
> Namespace or type 'Current' for the Imports
> 'System.Web.HTTPContext.Current' cannot be found.
>
> Imports System.Web.HTTPContext.Current
>
> and
>
> C:\Inetpub\wwwroot\staffingworkshop\emailold3.vb(1 0) : error BC30469:
> Reference to a non-shared member requires an object reference.
>
> dim URLPath As String = Left(request.path,
> InStrRev(request.path, "/") - 1)
>
> You, and the error, say I need to have an object reference. But when I
> create an aspx page (where I am using code inside), I don't need this. I
> can reference request (and MapPath) directly.
>
> I understood that the difference between code-inside and code-behind was
> that you had to explicitly do the imports in a code-behind file). But in
> this case, it is even more than that.
>
> That is where I am confused.
>
> Thanks,
>
> Tom
>>
>> hope that helps..
>>
>> Karl
>>
>> --
>> MY ASP.Net tutorials
>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>> annoying)
>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
>> come!)
>> "tshad" <(E-Mail Removed)> wrote in message
>> news:u0t5V%(E-Mail Removed)...
>>> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
>>> wrote in message news:O%238$(E-Mail Removed)...
>>>> You would create an instance class with instance members if you wanted
>>>> to create an Email object, set it's properties and have a Send()
>>>> method, ala:
>>>>
>>>> dim email as new Email("some subject")
>>>> email.Body = "xxx"
>>>> email.Send()
>>>>
>>>>
>>>> you would use a shared member if you wanted to pass everything in as
>>>> parameters
>>>>
>>>> EmailHelp.Send("some subject", "xxx")
>>>>
>>>> I personally prefer the syntax of the 2nd example...but if you wanted
>>>> to keep a bunch of different emails, say you wanted to cache them or
>>>> move them around between your layers, it'd be necessary to have
>>>> distinct instances.
>>>
>>> I prefer the 2nd example also.
>>>
>>> But when you talk about caching or moving them, are you saying that you
>>> want an instance so you could do something like:
>>>
>>> sub somefunction (myEmail as Email)
>>> ...
>>> myEmail.somemethod()
>>>
>>> end sub
>>>
>>> Then using your 1st example:
>>>
>>> dim email as new Email("some subject")
>>> email.Body = "xxx"
>>> email.Send()
>>> ...
>>> somefunction(email) ' calling the above function
>>>
>>>
>>> BTW, I took my email function and tried to compile it using:
>>>
>>> C:\Inetpub\wwwroot\staffingworkshop>vbc /t:library email.vb
>>> /r:system.web.dll /r:system.data.dll /r:system.dll
>>> /r:Microsoft.VisualBasic.dll
>>>
>>> and I get the following errors:
>>>
>>> C:\Inetpub\wwwroot\staffingworkshop\email.vb(49) : error BC30469:
>>> Reference to a non-shared member requires an object reference.
>>>
>>> I am getting this for request.path and for MapPath.
>>>
>>> My file looks like:
>>> ************************************************** ********************************
>>> Imports System
>>> Imports System.Web
>>> Imports System.IO
>>> Imports System.Web.UI
>>> Imports System.Web.SessionState
>>> Imports System.Web.Mail
>>> Imports System.Data
>>> Imports System.Data.SqlClient
>>> Imports System.Web.HttpCookie
>>> Imports System.Web.HttpCookieCollection
>>> Imports System.Web.HttpResponse
>>> Imports System.Web.HttpRequest
>>> Imports System.Web.HttpApplication
>>> Imports System.Web.HttpApplicationState
>>> Imports Microsoft.VisualBasic
>>>
>>> NameSpace MyFunctions
>>>
>>> Public Class Email
>>>
>>> Public Shared sub sendEmail ( )
>>> dim webMasterEmail As String
>>> dim emailSubject As String
>>> Dim mailServer As String
>>> Dim contactEmail As String
>>> Dim screenTestSubject As String
>>>
>>> Dim emailReader As SqlDataReader
>>>
>>> Dim ConnectionString as String
>>> =System.Configuration.ConfigurationSettings.AppSet tings("MM_CONNECTION_STRING_ftsolutions")
>>> Dim objConn as New SqlConnection (ConnectionString)
>>> Dim CommandText as String = "Select
>>> MailServer,WebMasterEmail,ContactEmail,ScreenTestS ubject from
>>> emailResponse where ClientID = '1234'"
>>> Dim objCmd as New SqlCommand(CommandText,objConn)
>>>
>>> objConn.Open()
>>>
>>> emailReader = objCmd.ExecuteReader
>>>
>>> if emailReader.Read then
>>> mailServer = emailReader("MailServer")
>>> webMasterEmail = emailReader("WebMasterEmail")
>>> contactEmail = emailReader("ContactEmail")
>>> screenTestSubject = emailReader("ScreenTestSubject")
>>> end If
>>>
>>> objConn.close()
>>>
>>> dim URLPath As String = _
>>> Left(request.path, InStrRev(request.path, "/") - 1)
>>>
>>> Dim objStreamReader as StreamReader
>>> Dim strInput As String
>>> Dim strBuffer As String
>>>
>>> If File.exists(MapPath("..\..\automail\new_account_au tomail.htm"))
>>> then
>>> objStreamReader =
>>> File.OpenText(MapPath("..\..\automail\new_account_ automail.htm"))
>>> strInput = objStreamReader.ReadLine()
>>> while strInput <> nothing
>>> strBuffer = strBuffer & strInput
>>> strInput = objStreamReader.ReadLine()
>>> end while
>>> objStreamReader.Close
>>> end if
>>>
>>> Dim Message As New MailMessage()
>>> message.To = contactEmail
>>> message.From = webMasterEmail
>>> message.Subject = screenTestSubject
>>> message.Body = "This is line 1<br><br>"
>>> message.Body = message.Body & "This is line 2<br><br>"
>>> strInput = strBuffer.replace("#MESSAGE#",message.Body)
>>> strInput = strInput.replace("#MAILTITLE#","Screen Test Confirmation")
>>> message.Body = strInput
>>> message.BodyFormat = MailFormat.Html
>>> SmtpMail.SmtpServer = mailServer
>>> smtpMail.Send(message)
>>> end sub
>>>
>>> End Class
>>>
>>> End Namespace
>>> ************************************************** **********************************
>>>
>>> This happens whether emailSend is Shared or not.
>>>
>>> Thanks,
>>>
>>> Tom
>>>
>>>>
>>>> Trust your gut feeling.
>>>>
>>>> Karl
>>>>
>>>> --
>>>> MY ASP.Net tutorials
>>>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>>>> annoying)
>>>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
>>>> come!)
>>>> "tshad" <(E-Mail Removed)> wrote in message
>>>> news:%(E-Mail Removed)...
>>>>> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
>>>>> net> wrote in message news:(E-Mail Removed)...
>>>>>> Tom:
>>>>>> Is the function manipulating instance data? Looking at your
>>>>>> functions, they look like they need to be shared.
>>>>>>
>>>>>> Think of it this way.
>>>>>>
>>>>>> You have a class called Car, which has a property named
>>>>>> AirbagDeployed as boolean
>>>>>>
>>>>>> if you have a function named DeployAirbag() it would need to be an
>>>>>> instance (non-shared) method. Why? Because you would have create a
>>>>>> new car instance and would want to deploy that particular car's
>>>>>> airbag. in other words, instnace methods behave against a particular
>>>>>> instance. Your BitHandling class looks like a helper function for
>>>>>> dealing with bit information. You wouldn't create separate instance
>>>>>> of them as you don't need to represent different bithandling (as you
>>>>>> would differnent cars)...ergo your function don't behave against
>>>>>> instances.
>>>>>>
>>>>>> A case where you might have instances is if your BitHandling was
>>>>>> culture-specific. In which case you might have different bithandling
>>>>>> instances per culture. In this case you'd need to create a new
>>>>>> BitHandling class (specifying the culture) and then your functions
>>>>>> would behave against that particular instance. (As an aside, an
>>>>>> alternative would be to pass the culture information to each shared
>>>>>> function so you wouldn't need to create culture-specific instances
>>>>>> which works fine for a single parameter, but becomes messy when
>>>>>> you're talking about more..)
>>>>>
>>>>> That's what gets confusing. Trying to figure out the instances bit
>>>>> (no pun intended) and if it is needed. Here is the other function
>>>>> that I am creating to put in my MyFunctions Namespace. I would give
>>>>> is a class of email, I suppose.
>>>>>
>>>>> This is just my prototype email program that I am putting together to
>>>>> make a more generalized function from.
>>>>>
>>>>> It, in essence:
>>>>> gets the connection to Sql
>>>>> gets the email record to get the subject and from/to addresses
>>>>> reads a text file to put into the body of the message
>>>>> adds a few things to the email
>>>>> sends the email
>>>>>
>>>>> I would probably change the function to something like:
>>>>>
>>>>> public shared sendEmail(filename as string, subject as string)
>>>>>
>>>>> ************************************************** *************************************
>>>>> sub sendEmail ( )
>>>>> dim webMasterEmail As String
>>>>> dim emailSubject As String
>>>>> Dim mailServer As String
>>>>> Dim contactEmail As String
>>>>> Dim screenTestSubject As String
>>>>>
>>>>> Dim emailReader As SqlDataReader
>>>>>
>>>>> Dim ConnectionString as String
>>>>> =System.Configuration.ConfigurationSettings.AppSet tings("MM_CONNECTION_STRING_solutions")
>>>>> Dim objConn as New SqlConnection (ConnectionString)
>>>>> Dim CommandText as String = "Select
>>>>> MailServer,WebMasterEmail,ContactEmail,ScreenTestS ubject from
>>>>> emailResponse where ClientID = '1234'"
>>>>> Dim objCmd as New SqlCommand(CommandText,objConn)
>>>>>
>>>>> objConn.Open()
>>>>>
>>>>> emailReader = objCmd.ExecuteReader
>>>>>
>>>>> if emailReader.Read then
>>>>> mailServer = emailReader("MailServer")
>>>>> webMasterEmail = emailReader("WebMasterEmail")
>>>>> contactEmail = emailReader("ContactEmail")
>>>>> screenTestSubject = emailReader("ScreenTestSubject")
>>>>> end If
>>>>>
>>>>> objConn.close()
>>>>>
>>>>> dim URLPath As String = _
>>>>> Left(request.path, InStrRev(request.path, "/") - 1)
>>>>>
>>>>> Dim objStreamReader as StreamReader
>>>>> Dim strInput As String
>>>>> Dim strBuffer As String
>>>>>
>>>>> If File.exists(MapPath("\new_account_automail.htm")) then
>>>>> objStreamReader = File.OpenText(MapPath("\new_account_automail.htm") )
>>>>> strInput = objStreamReader.ReadLine()
>>>>> while strInput <> nothing
>>>>> strBuffer = strBuffer & strInput
>>>>> strInput = objStreamReader.ReadLine()
>>>>> end while
>>>>> objStreamReader.Close
>>>>> end if
>>>>>
>>>>> Dim Message As New MailMessage()
>>>>> message.To = contactEmail
>>>>> message.From = webMasterEmail
>>>>> message.Subject = screenTestSubject
>>>>> message.Body = "This is line 1<br><br>"
>>>>> message.Body = message.Body & "This is line 2<br><br>"
>>>>> strInput = strBuffer.replace("#MESSAGE#",message.Body)
>>>>> strInput = strInput.replace("#MAILTITLE#","Screen Test Confirmation")
>>>>> message.Body = strInput
>>>>> message.BodyFormat = MailFormat.Html
>>>>> SmtpMail.SmtpServer = mailServer
>>>>> smtpMail.Send(message)
>>>>> end sub
>>>>> ************************************************** ***************************
>>>>>
>>>>> Would this work as a shared function, or would I need to make this an
>>>>> instance?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Tom
>>>>>>
>>>>>> Karl
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> MY ASP.Net tutorials
>>>>>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>>>>>> annoying)
>>>>>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more
>>>>>> to
>>>>>> come!)
>>>>>> "tshad" <(E-Mail Removed)> wrote in message
>>>>>> news:(E-Mail Removed)...
>>>>>>>I am setting up some of my functions in a class called MyFunctions.
>>>>>>>
>>>>>>> I am not clear as to the best time to set a function as Shared and
>>>>>>> when not to. For example, I have the following bit manipulation
>>>>>>> routines in my Class:
>>>>>>>
>>>>>>> ************************************************** *****************************
>>>>>>> imports System
>>>>>>>
>>>>>>> NameSpace MyFunctions
>>>>>>>
>>>>>>> Public Class BitHandling
>>>>>>>
>>>>>>> '*----------------------------------------------------------*
>>>>>>> '* Name : BitSet *
>>>>>>> '*----------------------------------------------------------*
>>>>>>> '* Purpose : Sets a given Bit in Number *
>>>>>>> '*----------------------------------------------------------*
>>>>>>> Public Shared Function BitSet(Number As Integer, _
>>>>>>> ByVal Bit As Integer) As Long
>>>>>>> If Bit = 31 Then
>>>>>>> Number = &H80000000 Or Number
>>>>>>> Else
>>>>>>> Number = (2 ^ Bit) Or Number
>>>>>>> End If
>>>>>>> BitSet = Number
>>>>>>> End Function
>>>>>>>
>>>>>>> '*----------------------------------------------------------*
>>>>>>> '* Name : BitClear *
>>>>>>> '*----------------------------------------------------------*
>>>>>>> '* Purpose : Clears a given Bit in Number *
>>>>>>> '*----------------------------------------------------------*
>>>>>>> Public Shared Function BitClear(Number As Integer, _
>>>>>>> ByVal Bit As Integer) As Long
>>>>>>> If Bit = 31 Then
>>>>>>> Number = &H7FFFFFFF And Number
>>>>>>> Else
>>>>>>> Number = ((2 ^ Bit) Xor &HFFFFFFFF) And Number
>>>>>>> End If
>>>>>>>
>>>>>>> BitClear = Number
>>>>>>> End Function
>>>>>>>
>>>>>>> '*----------------------------------------------------------*
>>>>>>> '* Name : BitIsSet *
>>>>>>> '*----------------------------------------------------------*
>>>>>>> '* Purpose : Test if bit 0 to bit 31 is set *
>>>>>>> '*----------------------------------------------------------*
>>>>>>> Public Shared Function BitIsSet(ByVal Number As Integer, _
>>>>>>> ByVal Bit As Integer) As Boolean
>>>>>>> BitIsSet = False
>>>>>>>
>>>>>>> If Bit = 31 Then
>>>>>>> If Number And &H80000000 Then BitIsSet = True
>>>>>>> Else
>>>>>>> If Number And (2 ^ Bit) Then BitIsSet = True
>>>>>>> End If
>>>>>>> End Function
>>>>>>>
>>>>>>> End Class
>>>>>>>
>>>>>>> End Namespace
>>>>>>>
>>>>>>> ************************************************** ******************************
>>>>>>>
>>>>>>> Now I have these set up as shared so I don't have to create an
>>>>>>> instance of the class:
>>>>>>>
>>>>>>> temp = BitHandling.BitSet(temp,3)
>>>>>>>
>>>>>>> vs.
>>>>>>>
>>>>>>> dim MyBits as new BitHandling
>>>>>>> temp = MyBits.BitSet(temp,3)
>>>>>>>
>>>>>>> I am also setting up my function to send out various emails which
>>>>>>> entails reading an Sql Record and reading a text file from disk, as
>>>>>>> well as sending the email:
>>>>>>>
>>>>>>> SmtpMail.SmtpServer = mailServer
>>>>>>> smtpMail.Send(message)
>>>>>>>
>>>>>>> What would tell me that I need to make this a non-shared function vs
>>>>>>> a shared one?
>>>>>>>
>>>>>>> Thanks,
>>>>>>>
>>>>>>> Tom
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
 
tshad
Guest
Posts: n/a
 
      05-27-2005
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:(E-Mail Removed)...
> Ok...
> Your codebehind file is a class which inherits Page.
>
> Page exposes the request/response/server which is why it just works as
> is..
>
>
> Your email class isn't "web aware" since it doesn't inherit from Page or
> any other "web aware" classes.
>
> To have webawarness you use System.Web.HttpContext.Current. You cannot
> import this because it's a property...you simply can't import properties
> in vb.net.
>
> youshould be doing:
>
> Imports System.Web
>
> and in your code use
> HttpContext.Current.Request
>
> or, as I showed earlier, use
> dim context as HttpContext = HttpContext.Current
> dim request as HttpRequest = context.Request
>
>
> We've covered a lot of ground in this thread


Yes, we have - and I really appreciate it. These are the things that are
hard to get a handle on as it isn't covered well in most of the books I have
(or at least not in the places I have been reading).

If you knew where to find this, it would probably be there - but you have to
find it.

Thanks,

Tom
>
> Karl
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is
> annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
> "tshad" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>
>> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
>> wrote in message news:(E-Mail Removed)...
>>> You've lost me a bit with the mass amounts of code.
>>>
>>> That's exactly what I'm saying about moving it around (as a parameter).
>>>
>>> As for your error. Objects which you typically program with in your
>>> page and user controls such as Request, Response, Server, ... are there
>>> because they are exposed as part of the System.Web.UI.Control class your
>>> page/user control inherits from (couple levels deep).
>>>
>>> Your email class inherits directly from Object, so Request, Response,
>>> Server are meaningless. To get references to these objects within your
>>> class functions, you need to use System.Web.HttpContext.Current which
>>> returns the current context (ie the web request) which exposes the
>>> Response, Request, Server, ...
>>>
>>> so you would do:
>>>
>>> dim context as HttpContext = HttpContext.Current
>>> if context is nothing then 'possible if someone is trying to use this
>>> class outside of a web-scope
>>> throw new InvalidOperationException("MyFunc needs to be called from web
>>> request") 'maybe you can do something other than throw an exception?
>>> end if
>>> 'now you have access to your request and response objects, here's an
>>> example
>>> dim request as HttpRequest = context.Request
>>> dim someValue as string =Request.QueryString("blah")
>>>

>>
>> I may be a little dense here.
>>
>> If I do this:
>>
>> ************************************************** ****************************
>> Imports System.Web.HTTPContext
>> Imports Microsoft.VisualBasic
>>
>> NameSpace MyFunctions
>>
>> Public Class Email
>>
>> Public Shared sub sendEmail ( )
>> dim URLPath As String = Left(Current.request.path,
>> InStrRev(Current.request.path, "/") - 1)
>> end sub
>>
>> End Class
>>
>> End Namespace
>> ************************************************** ****************************
>>
>> Where I use "Current.request.path" - it works fine.
>>
>>
>> If I do this:
>> ************************************************** ****************************
>> Imports System.Web.HTTPContext
>> Imports System.Web.HTTPContext.Current
>> Imports Microsoft.VisualBasic
>>
>> NameSpace MyFunctions
>>
>> Public Class Email
>>
>> Public Shared sub sendEmail ( )
>> dim URLPath As String = Left(request.path, InStrRev(request.path,
>> "/") - 1)
>> end sub
>>
>> End Class
>>
>> End Namespace
>> ************************************************** ****************************
>> Where I have "Imports System.Web.HTTPContext.Current", I get 2 errors:
>>
>> C:\Inetpub\wwwroot\staffingworkshop\emailold3.vb(2 ) : error BC30466:
>> Namespace or type 'Current' for the Imports
>> 'System.Web.HTTPContext.Current' cannot be found.
>>
>> Imports System.Web.HTTPContext.Current
>>
>> and
>>
>> C:\Inetpub\wwwroot\staffingworkshop\emailold3.vb(1 0) : error BC30469:
>> Reference to a non-shared member requires an object reference.
>>
>> dim URLPath As String = Left(request.path,
>> InStrRev(request.path, "/") - 1)
>>
>> You, and the error, say I need to have an object reference. But when I
>> create an aspx page (where I am using code inside), I don't need this. I
>> can reference request (and MapPath) directly.
>>
>> I understood that the difference between code-inside and code-behind was
>> that you had to explicitly do the imports in a code-behind file). But in
>> this case, it is even more than that.
>>
>> That is where I am confused.
>>
>> Thanks,
>>
>> Tom
>>>
>>> hope that helps..
>>>
>>> Karl
>>>
>>> --
>>> MY ASP.Net tutorials
>>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>>> annoying)
>>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
>>> come!)
>>> "tshad" <(E-Mail Removed)> wrote in message
>>> news:u0t5V%(E-Mail Removed)...
>>>> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
>>>> wrote in message news:O%238$(E-Mail Removed)...
>>>>> You would create an instance class with instance members if you wanted
>>>>> to create an Email object, set it's properties and have a Send()
>>>>> method, ala:
>>>>>
>>>>> dim email as new Email("some subject")
>>>>> email.Body = "xxx"
>>>>> email.Send()
>>>>>
>>>>>
>>>>> you would use a shared member if you wanted to pass everything in as
>>>>> parameters
>>>>>
>>>>> EmailHelp.Send("some subject", "xxx")
>>>>>
>>>>> I personally prefer the syntax of the 2nd example...but if you wanted
>>>>> to keep a bunch of different emails, say you wanted to cache them or
>>>>> move them around between your layers, it'd be necessary to have
>>>>> distinct instances.
>>>>
>>>> I prefer the 2nd example also.
>>>>
>>>> But when you talk about caching or moving them, are you saying that you
>>>> want an instance so you could do something like:
>>>>
>>>> sub somefunction (myEmail as Email)
>>>> ...
>>>> myEmail.somemethod()
>>>>
>>>> end sub
>>>>
>>>> Then using your 1st example:
>>>>
>>>> dim email as new Email("some subject")
>>>> email.Body = "xxx"
>>>> email.Send()
>>>> ...
>>>> somefunction(email) ' calling the above function
>>>>
>>>>
>>>> BTW, I took my email function and tried to compile it using:
>>>>
>>>> C:\Inetpub\wwwroot\staffingworkshop>vbc /t:library email.vb
>>>> /r:system.web.dll /r:system.data.dll /r:system.dll
>>>> /r:Microsoft.VisualBasic.dll
>>>>
>>>> and I get the following errors:
>>>>
>>>> C:\Inetpub\wwwroot\staffingworkshop\email.vb(49) : error BC30469:
>>>> Reference to a non-shared member requires an object reference.
>>>>
>>>> I am getting this for request.path and for MapPath.
>>>>
>>>> My file looks like:
>>>> ************************************************** ********************************
>>>> Imports System
>>>> Imports System.Web
>>>> Imports System.IO
>>>> Imports System.Web.UI
>>>> Imports System.Web.SessionState
>>>> Imports System.Web.Mail
>>>> Imports System.Data
>>>> Imports System.Data.SqlClient
>>>> Imports System.Web.HttpCookie
>>>> Imports System.Web.HttpCookieCollection
>>>> Imports System.Web.HttpResponse
>>>> Imports System.Web.HttpRequest
>>>> Imports System.Web.HttpApplication
>>>> Imports System.Web.HttpApplicationState
>>>> Imports Microsoft.VisualBasic
>>>>
>>>> NameSpace MyFunctions
>>>>
>>>> Public Class Email
>>>>
>>>> Public Shared sub sendEmail ( )
>>>> dim webMasterEmail As String
>>>> dim emailSubject As String
>>>> Dim mailServer As String
>>>> Dim contactEmail As String
>>>> Dim screenTestSubject As String
>>>>
>>>> Dim emailReader As SqlDataReader
>>>>
>>>> Dim ConnectionString as String
>>>> =System.Configuration.ConfigurationSettings.AppSet tings("MM_CONNECTION_STRING_ftsolutions")
>>>> Dim objConn as New SqlConnection (ConnectionString)
>>>> Dim CommandText as String = "Select
>>>> MailServer,WebMasterEmail,ContactEmail,ScreenTestS ubject from
>>>> emailResponse where ClientID = '1234'"
>>>> Dim objCmd as New SqlCommand(CommandText,objConn)
>>>>
>>>> objConn.Open()
>>>>
>>>> emailReader = objCmd.ExecuteReader
>>>>
>>>> if emailReader.Read then
>>>> mailServer = emailReader("MailServer")
>>>> webMasterEmail = emailReader("WebMasterEmail")
>>>> contactEmail = emailReader("ContactEmail")
>>>> screenTestSubject = emailReader("ScreenTestSubject")
>>>> end If
>>>>
>>>> objConn.close()
>>>>
>>>> dim URLPath As String = _
>>>> Left(request.path, InStrRev(request.path, "/") - 1)
>>>>
>>>> Dim objStreamReader as StreamReader
>>>> Dim strInput As String
>>>> Dim strBuffer As String
>>>>
>>>> If File.exists(MapPath("..\..\automail\new_account_au tomail.htm"))
>>>> then
>>>> objStreamReader =
>>>> File.OpenText(MapPath("..\..\automail\new_account_ automail.htm"))
>>>> strInput = objStreamReader.ReadLine()
>>>> while strInput <> nothing
>>>> strBuffer = strBuffer & strInput
>>>> strInput = objStreamReader.ReadLine()
>>>> end while
>>>> objStreamReader.Close
>>>> end if
>>>>
>>>> Dim Message As New MailMessage()
>>>> message.To = contactEmail
>>>> message.From = webMasterEmail
>>>> message.Subject = screenTestSubject
>>>> message.Body = "This is line 1<br><br>"
>>>> message.Body = message.Body & "This is line 2<br><br>"
>>>> strInput = strBuffer.replace("#MESSAGE#",message.Body)
>>>> strInput = strInput.replace("#MAILTITLE#","Screen Test Confirmation")
>>>> message.Body = strInput
>>>> message.BodyFormat = MailFormat.Html
>>>> SmtpMail.SmtpServer = mailServer
>>>> smtpMail.Send(message)
>>>> end sub
>>>>
>>>> End Class
>>>>
>>>> End Namespace
>>>> ************************************************** **********************************
>>>>
>>>> This happens whether emailSend is Shared or not.
>>>>
>>>> Thanks,
>>>>
>>>> Tom
>>>>
>>>>>
>>>>> Trust your gut feeling.
>>>>>
>>>>> Karl
>>>>>
>>>>> --
>>>>> MY ASP.Net tutorials
>>>>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>>>>> annoying)
>>>>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
>>>>> come!)
>>>>> "tshad" <(E-Mail Removed)> wrote in message
>>>>> news:%(E-Mail Removed)...
>>>>>> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
>>>>>> net> wrote in message news:(E-Mail Removed)...
>>>>>>> Tom:
>>>>>>> Is the function manipulating instance data? Looking at your
>>>>>>> functions, they look like they need to be shared.
>>>>>>>
>>>>>>> Think of it this way.
>>>>>>>
>>>>>>> You have a class called Car, which has a property named
>>>>>>> AirbagDeployed as boolean
>>>>>>>
>>>>>>> if you have a function named DeployAirbag() it would need to be an
>>>>>>> instance (non-shared) method. Why? Because you would have create a
>>>>>>> new car instance and would want to deploy that particular car's
>>>>>>> airbag. in other words, instnace methods behave against a particular
>>>>>>> instance. Your BitHandling class looks like a helper function for
>>>>>>> dealing with bit information. You wouldn't create separate instance
>>>>>>> of them as you don't need to represent different bithandling (as you
>>>>>>> would differnent cars)...ergo your function don't behave against
>>>>>>> instances.
>>>>>>>
>>>>>>> A case where you might have instances is if your BitHandling was
>>>>>>> culture-specific. In which case you might have different bithandling
>>>>>>> instances per culture. In this case you'd need to create a new
>>>>>>> BitHandling class (specifying the culture) and then your functions
>>>>>>> would behave against that particular instance. (As an aside, an
>>>>>>> alternative would be to pass the culture information to each shared
>>>>>>> function so you wouldn't need to create culture-specific instances
>>>>>>> which works fine for a single parameter, but becomes messy when
>>>>>>> you're talking about more..)
>>>>>>
>>>>>> That's what gets confusing. Trying to figure out the instances bit
>>>>>> (no pun intended) and if it is needed. Here is the other function
>>>>>> that I am creating to put in my MyFunctions Namespace. I would give
>>>>>> is a class of email, I suppose.
>>>>>>
>>>>>> This is just my prototype email program that I am putting together to
>>>>>> make a more generalized function from.
>>>>>>
>>>>>> It, in essence:
>>>>>> gets the connection to Sql
>>>>>> gets the email record to get the subject and from/to addresses
>>>>>> reads a text file to put into the body of the message
>>>>>> adds a few things to the email
>>>>>> sends the email
>>>>>>
>>>>>> I would probably change the function to something like:
>>>>>>
>>>>>> public shared sendEmail(filename as string, subject as string)
>>>>>>
>>>>>> ************************************************** *************************************
>>>>>> sub sendEmail ( )
>>>>>> dim webMasterEmail As String
>>>>>> dim emailSubject As String
>>>>>> Dim mailServer As String
>>>>>> Dim contactEmail As String
>>>>>> Dim screenTestSubject As String
>>>>>>
>>>>>> Dim emailReader As SqlDataReader
>>>>>>
>>>>>> Dim ConnectionString as String
>>>>>> =System.Configuration.ConfigurationSettings.AppSet tings("MM_CONNECTION_STRING_solutions")
>>>>>> Dim objConn as New SqlConnection (ConnectionString)
>>>>>> Dim CommandText as String = "Select
>>>>>> MailServer,WebMasterEmail,ContactEmail,ScreenTestS ubject from
>>>>>> emailResponse where ClientID = '1234'"
>>>>>> Dim objCmd as New SqlCommand(CommandText,objConn)
>>>>>>
>>>>>> objConn.Open()
>>>>>>
>>>>>> emailReader = objCmd.ExecuteReader
>>>>>>
>>>>>> if emailReader.Read then
>>>>>> mailServer = emailReader("MailServer")
>>>>>> webMasterEmail = emailReader("WebMasterEmail")
>>>>>> contactEmail = emailReader("ContactEmail")
>>>>>> screenTestSubject = emailReader("ScreenTestSubject")
>>>>>> end If
>>>>>>
>>>>>> objConn.close()
>>>>>>
>>>>>> dim URLPath As String = _
>>>>>> Left(request.path, InStrRev(request.path, "/") - 1)
>>>>>>
>>>>>> Dim objStreamReader as StreamReader
>>>>>> Dim strInput As String
>>>>>> Dim strBuffer As String
>>>>>>
>>>>>> If File.exists(MapPath("\new_account_automail.htm")) then
>>>>>> objStreamReader =
>>>>>> File.OpenText(MapPath("\new_account_automail.htm") )
>>>>>> strInput = objStreamReader.ReadLine()
>>>>>> while strInput <> nothing
>>>>>> strBuffer = strBuffer & strInput
>>>>>> strInput = objStreamReader.ReadLine()
>>>>>> end while
>>>>>> objStreamReader.Close
>>>>>> end if
>>>>>>
>>>>>> Dim Message As New MailMessage()
>>>>>> message.To = contactEmail
>>>>>> message.From = webMasterEmail
>>>>>> message.Subject = screenTestSubject
>>>>>> message.Body = "This is line 1<br><br>"
>>>>>> message.Body = message.Body & "This is line 2<br><br>"
>>>>>> strInput = strBuffer.replace("#MESSAGE#",message.Body)
>>>>>> strInput = strInput.replace("#MAILTITLE#","Screen Test Confirmation")
>>>>>> message.Body = strInput
>>>>>> message.BodyFormat = MailFormat.Html
>>>>>> SmtpMail.SmtpServer = mailServer
>>>>>> smtpMail.Send(message)
>>>>>> end sub
>>>>>> ************************************************** ***************************
>>>>>>
>>>>>> Would this work as a shared function, or would I need to make this an
>>>>>> instance?
>>>>>>
>>>>>> Thanks,
>>>>>>
>>>>>> Tom
>>>>>>>
>>>>>>> Karl
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> MY ASP.Net tutorials
>>>>>>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>>>>>>> annoying)
>>>>>>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more
>>>>>>> to
>>>>>>> come!)
>>>>>>> "tshad" <(E-Mail Removed)> wrote in message
>>>>>>> news:(E-Mail Removed)...
>>>>>>>>I am setting up some of my functions in a class called MyFunctions.
>>>>>>>>
>>>>>>>> I am not clear as to the best time to set a function as Shared and
>>>>>>>> when not to. For example, I have the following bit manipulation
>>>>>>>> routines in my Class:
>>>>>>>>
>>>>>>>> ************************************************** *****************************
>>>>>>>> imports System
>>>>>>>>
>>>>>>>> NameSpace MyFunctions
>>>>>>>>
>>>>>>>> Public Class BitHandling
>>>>>>>>
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> '* Name : BitSet *
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> '* Purpose : Sets a given Bit in Number *
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> Public Shared Function BitSet(Number As Integer, _
>>>>>>>> ByVal Bit As Integer) As Long
>>>>>>>> If Bit = 31 Then
>>>>>>>> Number = &H80000000 Or Number
>>>>>>>> Else
>>>>>>>> Number = (2 ^ Bit) Or Number
>>>>>>>> End If
>>>>>>>> BitSet = Number
>>>>>>>> End Function
>>>>>>>>
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> '* Name : BitClear *
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> '* Purpose : Clears a given Bit in Number *
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> Public Shared Function BitClear(Number As Integer, _
>>>>>>>> ByVal Bit As Integer) As Long
>>>>>>>> If Bit = 31 Then
>>>>>>>> Number = &H7FFFFFFF And Number
>>>>>>>> Else
>>>>>>>> Number = ((2 ^ Bit) Xor &HFFFFFFFF) And Number
>>>>>>>> End If
>>>>>>>>
>>>>>>>> BitClear = Number
>>>>>>>> End Function
>>>>>>>>
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> '* Name : BitIsSet *
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> '* Purpose : Test if bit 0 to bit 31 is set *
>>>>>>>> '*----------------------------------------------------------*
>>>>>>>> Public Shared Function BitIsSet(ByVal Number As Integer, _
>>>>>>>> ByVal Bit As Integer) As Boolean
>>>>>>>> BitIsSet = False
>>>>>>>>
>>>>>>>> If Bit = 31 Then
>>>>>>>> If Number And &H80000000 Then BitIsSet = True
>>>>>>>> Else
>>>>>>>> If Number And (2 ^ Bit) Then BitIsSet = True
>>>>>>>> End If
>>>>>>>> End Function
>>>>>>>>
>>>>>>>> End Class
>>>>>>>>
>>>>>>>> End Namespace
>>>>>>>>
>>>>>>>> ************************************************** ******************************
>>>>>>>>
>>>>>>>> Now I have these set up as shared so I don't have to create an
>>>>>>>> instance of the class:
>>>>>>>>
>>>>>>>> temp = BitHandling.BitSet(temp,3)
>>>>>>>>
>>>>>>>> vs.
>>>>>>>>
>>>>>>>> dim MyBits as new BitHandling
>>>>>>>> temp = MyBits.BitSet(temp,3)
>>>>>>>>
>>>>>>>> I am also setting up my function to send out various emails which
>>>>>>>> entails reading an Sql Record and reading a text file from disk, as
>>>>>>>> well as sending the email:
>>>>>>>>
>>>>>>>> SmtpMail.SmtpServer = mailServer
>>>>>>>> smtpMail.Send(message)
>>>>>>>>
>>>>>>>> What would tell me that I need to make this a non-shared function
>>>>>>>> vs a shared one?
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>>
>>>>>>>> Tom
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
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
How far are "Shared" objects and properties shared? HmFireBall ASP .Net 2 01-02-2006 01:02 PM
Global Shared functions, shared variables Imran Aziz ASP .Net 1 08-22-2005 06:43 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