''* ---------------------------------------------- -------- ---- * ''* ---------------------------------------------- -------- ---- * ''* ---------------------------------------------- -------- ---- * ''*姓名:BitClear * ''* --------------------------- ------------------------------- * ''*目的:清除数字中的给定位* ''* --------------------------------------------- ------------- * 公共共享函数BitClear(数字为整数,_ ByVal位为整数)为长如果位= 31那么数字=& H7FFFFFFF和数字否则数字=((2 ^位)Xor& HFFFFFFFF)和数字结束如果 BitClear = Number 结束功能 ''* --------------------------------- ------------------------- * ''*姓名:BitIsSet * ''* ------ -------------------------------------------------- - * ''*目的:测试是否设置了第0位到第31位* ''* --------------------- ------------------------------------- * 公共共享函数BitIsSet(ByVal Number As Integer,_ ByVal Bit As Integer)As Boolean BitIsSet = False 如果Bit = 31那么如果数字和& H80000000然后BitIsSet =真否则如果数字和(2 ^位)然后BitIsSet =真结束如果结束功能 结束等级 结束命名空间 ******************************* ******************* ****************************** 现在我将这些设置为共享,所以我不必创建类的实例: temp = BitHandling.BitSet(temp, 3) 暗淡的MyBits作为新的BitHandling temp = MyBits.BitSet(temp,3) 我也是设置我的功能,发送各种电子邮件,这些电子邮件需要读取Sql记录并从磁盘读取文本文件,以及发送电子邮件: SmtpMail.SmtpServer = mailServer smtpMail.Send(消息) 什么会告诉我,我需要将这个作为非共享功能而不是共享功能? 谢谢, Tom 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 notto. For example, I have the following bit manipulation routines in myClass:************************************************** *****************************imports SystemNameSpace MyFunctionsPublic Class BitHandling''*----------------------------------------------------------*''* Name : BitSet *''*----------------------------------------------------------*''* Purpose : Sets a given Bit in Number *''*----------------------------------------------------------*Public Shared Function BitSet(Number As Integer, _ByVal Bit As Integer) As LongIf Bit = 31 ThenNumber = &H80000000 Or NumberElseNumber = (2 ^ Bit) Or NumberEnd IfBitSet = NumberEnd Function''*----------------------------------------------------------*''* Name : BitClear *''*----------------------------------------------------------*''* Purpose : Clears a given Bit in Number *''*----------------------------------------------------------*Public Shared Function BitClear(Number As Integer, _ByVal Bit As Integer) As LongIf Bit = 31 ThenNumber = &H7FFFFFFF And NumberElseNumber = ((2 ^ Bit) Xor &HFFFFFFFF) And NumberEnd IfBitClear = NumberEnd 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 BooleanBitIsSet = FalseIf Bit = 31 ThenIf Number And &H80000000 Then BitIsSet = TrueElseIf Number And (2 ^ Bit) Then BitIsSet = TrueEnd IfEnd FunctionEnd ClassEnd Namespace************************************************** ******************************Now I have these set up as shared so I don''t have to create an instance ofthe class:temp = BitHandling.BitSet(temp,3)vs.dim MyBits as new BitHandlingtemp = MyBits.BitSet(temp,3)I am also setting up my function to send out various emails which entailsreading an Sql Record and reading a text file from disk, as well as sendingthe email:SmtpMail.SmtpServer = mailServersmtpMail.Send(message)What would tell me that I need to make this a non-shared function vs ashared one?Thanks,Tom 解决方案 Tom:Is the function manipulating instance data? Looking at your functions, theylook like they need to be shared.Think of it this way.You have a class called Car, which has a property named AirbagDeployed asbooleanif you have a function named DeployAirbag() it would need to be aninstance (non-shared) method. Why? Because you would have create a new carinstance and would want to deploy that particular car''s airbag. in otherwords, instnace methods behave against a particular instance. YourBitHandling class looks like a helper function for dealing with bitinformation. You wouldn''t create separate instance of them as you don''tneed to represent different bithandling (as you would differnentcars)...ergo your function don''t behave against instances.A case where you might have instances is if your BitHandling wasculture-specific. In which case you might have different bithandlinginstances per culture. In this case you''d need to create a new BitHandlingclass (specifying the culture) and then your functions would behave againstthat particular instance. (As an aside, an alternative would be to pass theculture information to each shared function so you wouldn''t need to createculture-specific instances which works fine for a single parameter, butbecomes messy when you''re talking about more..)Karl--MY ASP.Net tutorials http://www.openmymind.net/ - New and Improved (yes, the popup isannoying) http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more tocome!)"tshad" <ts**********@ftsolutions.com> wrote in messagenews:uB**************@tk2msftngp13.phx.gbl...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"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>wrote in message news:e5**************@TK2MSFTNGP12.phx.gbl... 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 punintended) and if it is needed. Here is the other function that I amcreating to put in my MyFunctions Namespace. I would give is a class ofemail, I suppose.This is just my prototype email program that I am putting together to make amore generalized function from.It, in essence:gets the connection to Sqlgets the email record to get the subject and from/to addressesreads a text file to put into the body of the messageadds a few things to the emailsends the emailI would probably change the function to something like:public shared sendEmail(filename as string, subject as string)************************************************** *************************************sub sendEmail ( )dim webMasterEmail As Stringdim emailSubject As StringDim mailServer As StringDim contactEmail As StringDim screenTestSubject As StringDim emailReader As SqlDataReaderDim ConnectionString as String=System.Configuration.ConfigurationSettings.AppSet tings("MM_CONNECTION_STRING_solutions")Dim objConn as New SqlConnection (ConnectionString)Dim CommandText as String = "SelectMailServer,WebMasterEmail,ContactEmail,ScreenTestS ubject from emailResponsewhere ClientID = ''1234''"Dim objCmd as New SqlCommand(CommandText,objConn)objConn.Open()emailReader = objCmd.ExecuteReaderif emailReader.Read thenmailServer = emailReader("MailServer")webMasterEmail = emailReader("WebMasterEmail")contactEmail = emailReader("ContactEmail")screenTestSubject = emailReader("ScreenTestSubject")end IfobjConn.close()dim URLPath As String = _Left(request.path, InStrRev(request.path, "/") - 1)Dim objStreamReader as StreamReaderDim strInput As StringDim strBuffer As StringIf File.exists(MapPath("\new_account_automail.htm")) thenobjStreamReader = File.OpenText(MapPath("\new_account_automail.htm") )strInput = objStreamReader.ReadLine()while strInput <> nothingstrBuffer = strBuffer & strInputstrInput = objStreamReader.ReadLine()end whileobjStreamReader.Closeend ifDim Message As New MailMessage()message.To = contactEmailmessage.From = webMasterEmailmessage.Subject = screenTestSubjectmessage.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 = strInputmessage.BodyFormat = MailFormat.HtmlSmtpMail.SmtpServer = mailServersmtpMail.Send(message)end sub************************************************** ***************************Would this work as a shared function, or would I need to make this aninstance?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" <ts**********@ftsolutions.com> wrote in message news:uB**************@tk2msftngp13.phx.gbl...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, TomYou would create an instance class with instance members if you wanted tocreate 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 asparametersEmailHelp.Send("some subject", "xxx")I personally prefer the syntax of the 2nd example...but if you wanted tokeep a bunch of different emails, say you wanted to cache them or move themaround between your layers, it''d be necessary to have distinct instances.Trust your gut feeling.Karl--MY ASP.Net tutorials http://www.openmymind.net/ - New and Improved (yes, the popup isannoying) http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more tocome!)"tshad" <ts**********@ftsolutions.com> wrote in messagenews:%2****************@TK2MSFTNGP09.phx.gbl... "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:e5**************@TK2MSFTNGP12.phx.gbl... 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" <ts**********@ftsolutions.com> wrote in message news:uB**************@tk2msftngp13.phx.gbl...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 这篇关于共享功能与非共享功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!