共享功能与非共享功能 [英] Shared functions vs Non-Shared Functions

查看:142
本文介绍了共享功能与非共享功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为MyFunctions的类中设置了一些函数。


我不清楚将函数设置为Shared的最佳时间以及何时不是

到。例如,我在我的

类中有以下位操作例程:


**************** ********************************** **************** *************

进口系统


NameSpace MyFunctions


公开Class BitHandling


''* --------------------------------- ------------------------- *

''*姓名:BitSet *

' * ------------------------------------------------ ---------- *

''*目的:设定数字中的给定位*

''* -------- -------------------------------------------------- *

公共共享函数BitSet(数字为整数,_

ByVal位为整数)为长

如果位= 31则

数量=& H80000000或数字

否则

数字=(2 ^位)或数字

结束如果

BitSet =数字

结束功能


''* ----------------------------------------- ----------------- *

''*姓名:BitClear *

''* ------ -------------------------------------------------- - *

''*目的:清除数字中的给定位*

''* ---------------- ------------------------------------------ *

公共共享函数BitClear(数字为整数,_

ByVal位为整数)为长

如果位= 31那么

数= =& ; H7FFFFFFF和数字

否则

数字=((2 ^位)Xor& HFFFFFFFF)和数字

结束如果


BitClear =数字

结束功能


''* -------------- -------------------------------------------- *

''*姓名:BitIsSet *

''* ----------------------------- ----------------------------- *

''*目的:测试如果设置位0到位31 *

''* ------------------------------ ---------------------------- *

公共共享函数BitIsSet(ByVal Number As Integer,_

ByVal Bit As Integer)As Boolean

BitIsSet = False


如果位= 31那么

如果数字和& H80000000然后BitIsSet =真

否则

如果数字和(2 ^位)然后BitIsSet =真

结束如果

结束功能


结束班级


结束命名空间


** ************************************************ ** ****************************


现在我将这些设置为共享,以便我不必在类中创建一个

的实例:


temp = BitHandling.BitSet(temp,3)

vs.


dim MyBits作为新的BitHandling

temp = MyBits.BitSet(temp,3)


我也在设置我的功能,发送各种电子邮件ntails

读取Sql Record并从磁盘读取文本文件,以及发送

电子邮件:


SmtpMail。 smtpServer = mailServer

smtpMail.Send(消息)


什么会告诉我,我需要使这个非共享功能vs

共享一个?


谢谢,


Tom

解决方案

Tom:

函数是否在操纵实例数据?看看你的功能,他们看起来好像需要分享。


想一想。


你有一个名为Car的类,其中有一个名为AirbagDeployed的属性为

boolean


实例(非共享)方法。为什么?因为您可以创建一辆新车

实例,并希望部署该特定车型的安全气囊。在其他

字中,instnace方法针对特定实例表现。你的

BitHandling类看起来像是一个辅助函数,用于处理位

信息。你不会创建它们的单独实例,因为你不需要
需要代表不同的位处理(因为你会不同的

汽车)...你的功能如何不要反对实例。


如果你的BitHandling是b / b $ b文化特定的,你可能有实例的情况。在这种情况下,您可能每种文化都有不同的位处理

实例。在这种情况下,你需要创建一个新的BitHandling

类(指定文化),然后你的函数将违反

特定实例。 (顺便说一句,另一种方法是将

文化信息传递给每个共享函数,这样你就不需要创建

文化特定的实例,它们工作正常对于一个参数,但是当你谈论更多时,

变得凌乱..)


Karl


-

我的ASP.Net教程
http: //www.openmymind.net/ - 新增和改进(是的,弹出窗口是

烦人)
http://www.openmymind.net/faq.aspx - 非官方新闻组常见问题解答(更多内容

来了! )

" tshad" < TS ********** @ ftsolutions.com>在消息中写道

news:uB ************** @ tk2msftngp13.phx.gbl ...

我正在设置一些我的函数在一个名为MyFunctions的类中。

我不清楚将函数设置为Shared的最佳时间以及
不是。例如,我在
类中有以下位操作例程:

************************ ************************** ************************ *****
进口系统

NameSpace MyFunctions

公共类BitHandling

''* ------- -------------------------------------------------- - *
''*姓名:BitSet *
''* ------------------------------ ---------------------------- *
''*目的:设置数字中的给定位*
' * ------------------------------------------------ ---------- *
公共共享函数BitSet(数字为整数,_
ByVal位为整数)为长
如果位= 31那么
数=& H80000000或数字
否则
数=(2 ^位)或数字
结束如果
BitSet =数字
结束功能
''* ---------------------------------------------- -------- ---- *
''*姓名: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


" Karl Seguin" < karl REMOVE @ REMOVE openmymind REMOVEMETOO。 ANDME net>

在消息新闻中写道:e5 ************** @ TK2MSFTNGP12.phx.gbl ...

Tom:
该函数是否操纵实例数据?看看你的功能,
看起来他们需要分享。

想一想。

你有一个名为Car的课程,它有一个名为AirbagDeployed的属性为
boolean
如果你有一个名为DeployAirbag()的函数,它需要是一个
实例(非共享)方法。为什么?因为你会创建一个新的
汽车实例,并希望部署该特定汽车的安全气囊。换句话说,instnace方法违背特定实例。你的BitHandling类看起来像一个辅助函数来处理位信息。你不会创建它们的单独实例,因为你不需要代表不同的位操作(因为你会不同的汽车)...因此你的功能不会与实例相反。

如果您的BitHandling是特定于文化的,那么您可能拥有实例的情况。在这种情况下,您可能每种文化都有不同的位处理实例。在这种情况下,您需要创建一个新的BitHandling类(指定文化),然后您的函数将针对该特定实例进行操作。 (顺便说一句,另一种方法是将文化信息传递给每个共享函数,这样你就不需要创建特定于文化的实例,这些实例适用于单个参数但是当你谈论更多时会变得凌乱..)


这就是令人困惑的事情。试图找出实例位(没有双关语
意图)以及是否需要。这是我创建放入MyFunctions命名空间的另一个函数。我想给我的是一类

的电子邮件。


这只是我的原型电子邮件程序,我正在整理它来制作一个

更广义的功能来自。


它本质上是:

获得与Sql的连接

获取电子邮件记录以获取主题和来自/到地址

读取文本文件以放入邮件正文

在电子邮件中添加一些内容

发送电子邮件


我可能会将功能更改为:


公共共享sendEmail(文件名为字符串,主题作为字符串)


*********************************** *************** *********************************** **

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 Sq lDataReader


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

其中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")

结束如果


objConn.close()


dim URLPath As String = _

Left( REQ uest.path,InStrRev(request.path," /") - 1)


Dim objStreamReader as StreamReader

Dim strInput As String

Dim strBuffer As String


如果File.exists(MapPath(" \ new_account_automail.htm"))那么

objStreamReader = File .OpenText(MapPath(" \ new_account_automail.htm"))

strInput = objStreamReader.ReadLine()

而strInput<>什么都没有

strBuffer = strBuffer& strInput

strInput = objStreamReader.ReadLine()

结束时

objStreamReader.Close

结束如果


Dim Message As New MailMessage()

message.To = contactEmail

message.From = webMasterEmail

message.Subject = screenTestSubject

message.Body ="这是第1行< br>< br>"

message.Body = message.Body& 这是第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(消息)

end sub

******* ******************************************* ******* ********************


这是否可以作为共享功能使用,还是需要将其作为一个共享功能/>
实例?


谢谢,


Tom
Karl

-
我的ASP.Net教程
http://www.openmymind.net/ - 新增和改进(是的,弹出窗口令人讨厌)
http://www.openmymind.net/faq.aspx - 非正式的n ewsgroup常见问题解答(更多内容来了!)
" tshad" < TS ********** @ ftsolutions.com>在消息中写道
新闻:uB ************** @ tk2msftngp13.phx.gbl ...

我正在设置我的一些功能一个名为MyFunctions的类。

我不清楚将函数设置为Shared的最佳时间以及
不是。例如,我的课程中有以下位操作例程:

************************ ************************** ************************ *****
进口系统

NameSpace MyFunctions

公共类BitHandling

''* ------- -------------------------------------------------- - *
''*姓名:BitSet *
''* ------------------------------ ---------------------------- *
''*目的:设置数字中的给定位*
' * ------------------------------------------------ ---------- *
公共共享函数BitSet(数字为整数,_
ByVal位为整数)为长
如果位= 31那么
数=& H80000000或数字
否则
数=(2 ^位)或数字
结束如果
BitSet =数字
结束功能
''* ---------------------------------------------- -------- ---- *
''*姓名: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



如果你想创建一个Email对象,你可以用实例成员创建一个实例类,设置它的属性并有一个Send()方法,ala:


暗淡的电子邮件作为新的电子邮件(一些主题)

电子邮件。 Body =" xxx"

email.Send()

如果你想传递所有内容,你会使用共享成员

参数


EmailHelp.Send(" some subject"," xxx")


我个人更喜欢第二个例子的语法..但是如果你想

保留一堆不同的电子邮件,说你想要缓存它们或移动它们

你的图层之间,它是必要的有不同的实例。


相信你的直觉。


Karl


-

我的ASP.Net教程
http://www.openmymind.net/ - 新增和改进(是的,弹出窗口是

烦人)
http://www.openmymind.net/faq.aspx - 非官方新闻组常见问题解答(更多到

来吧!)

" tshad" < TS ********** @ ftsolutions.com>在消息中写道

news:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

" Karl Seguin < karl REMOVE @ REMOVE openmymind REMOVEMETOO。 ANDME net>
在消息新闻中写道:e5 ************** @ TK2MSFTNGP12.phx.gbl ...

Tom:
该函数是否操纵实例数据?看看你的功能,
看起来他们需要分享。

想一想。

你有一个名为Car的课程,它有一个名为AirbagDeployed的属性为
boolean
如果你有一个名为DeployAirbag()的函数,它需要是一个
实例(非共享)方法。为什么?因为你会创建一个新的
汽车实例,并希望部署该特定汽车的安全气囊。换句话说,instnace方法违背特定实例。你的BitHandling类看起来像一个辅助函数来处理位信息。你不会创建它们的单独实例,因为你不需要代表不同的位操作(因为你会不同的汽车)...因此你的功能不会与实例相反。

如果您的BitHandling是特定于文化的,那么您可能拥有实例的情况。在这种情况下,您可能每种文化都有不同的位处理实例。在这种情况下,您需要创建一个新的BitHandling类(指定文化),然后您的函数将针对该特定实例进行操作。 (顺便说一句,另一种方法是将文化信息传递给每个共享函数,这样你就不需要创建特定于文化的实例,这些实例适用于单个参数但是当你谈论更多时会变得凌乱..)



这就是令人困惑的事情。试图找出实例位(没有
双关语)以及是否需要。这是我在MyFunctions命名空间中创建的另一个函数。我想,我会给出一类
电子邮件。

这只是我的原型电子邮件程序,我正在整理它以使
成为更广泛的功能。 />
它,实质上:
获取与Sql的连接
获取电子邮件记录以获取主题和从/到地址
读取文本文件放入正文的消息
为电子邮件添加了一些东西
发送电子邮件

我可能会将功能更改为:

公共共享sendEmail (文件名为字符串,主题为字符串)

********************************* ***************** ********************************* ****
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.C onfigurationSettings.AppSet tings(MM_CONNECTION_STRING_solutions)
将objConn作为新的SqlConnection(ConnectionString)将Dim CommandText命名为String ="选择
MailServer,WebMasterEmail,ContactEmail,ScreenTestS ubject from
emailResponse,其中ClientID =''1234''"
Dim objCmd为新SqlCommand(CommandText,objConn)

objConn.Open()

emailReader = objCmd .ExecuteReader

如果emailReader.Read那么
mailServer = emailReader(" MailServer")
webMasterEmail = emailReader(&#WebMasterEmail")
contactEmail = emailReader(" ; ContactEmail")
screenTestSubject = emailReader(" ScreenTestSubject")
结束如果

objConn.close()

dim URLPath As String = _
Left(request.path,InStrRev(request.path," /") - 1)

将objStreamReader变为StreamReader
Dim strInput As String
Dim strBuffer As String

如果File.exists(MapPath(" \ new_account_automail.htm"))然后
objStreamReader = File.OpenText(MapPath(" \ new_account_automail.htm"))
strInput = objStreamReader.ReadLine()
同时strInput<>什么都没有
strBuffer = strBuffer& strInput
strInput = objStreamReader.ReadLine()
结束时
objStreamReader.Close
结束如果

昏暗的消息作为新的MailMessage()
message.To = contactEmail
message.From = webMasterEmail
message.Subject = screenTestSubject
message.Body ="这是第1行< br>< br>"
message.Body = message.Body& 这是第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 )
结束分
************************************* *********** *****************************

这是否有效一个共享的功能,还是我需要把它作为一个
实例?

谢谢,

Tom


Karl

-
我的ASP.Net教程
http://www.openmymind.net/ - 新增和改进(是的,弹出窗口令人讨厌)
http://www.openmymind.net/faq.aspx - 非官方新闻组常见问题解答(更多内容
com e!)
tshad < TS ********** @ ftsolutions.com>在消息中写道
新闻:uB ************** @ tk2msftngp13.phx.gbl ...

我正在设置我的一些功能一个名为MyFunctions的类。

我不清楚将函数设置为Shared的最佳时间以及
不是。例如,我的课程中有以下位操作例程:

************************ ************************** ************************ *****
进口系统

NameSpace MyFunctions

公共类BitHandling

''* ------- -------------------------------------------------- - *
''*姓名:BitSet *
''* ------------------------------ ---------------------------- *
''*目的:设置数字中的给定位*
' * ------------------------------------------------ ---------- *
公共共享函数BitSet(数字为整数,_
ByVal位为整数)为长
如果位= 31那么
数=& H80000000或数字
否则
数=(2 ^位)或数字
结束如果
BitSet =数字
结束功能
''* ---------------------------------------------- -------- ---- *
''*姓名: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 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

解决方案

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

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



"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




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.

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" <ts**********@ftsolutions.com> wrote in message
news:%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





这篇关于共享功能与非共享功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆