vb.net VB 用什么替换了函数“Set”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1468890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What did VB replace the function "Set" with?
提问by ChaosPandion
I've found several aspx codes for forms which include the use of a "Set" function. When I try them out on the hosting server, I get an error message that "Set is no longer supported". Anyone know what replaced the "Set" command?
我发现了几个用于表单的 aspx 代码,其中包括使用“设置”函数。当我在托管服务器上试用它们时,我收到一条错误消息“不再支持 Set”。任何人都知道什么取代了“设置”命令?
More specifically, how do I change this:
更具体地说,我如何改变这一点:
Dim mail
Set mail = Server.CreateObject("CDONTS.NewMail")
mail.To = EmailTo
mail.From = EmailFrom
mail.Subject = Subject
mail.Body = Body
mail.Send
to be VB.NET compatible?
与 VB.NET 兼容?
回答by ChaosPandion
If you mean the VB6 syntax
如果您指的是 VB6 语法
Set obj = new Object
then you can simply remove the Set
那么你可以简单地删除 Set
obj = new Object()
回答by Rohan West
Set is a keyword in VB6, with the intrudction of VB.NET the keyword, as used in this context, was removed.
Set 是 VB6 中的关键字,随着 VB.NET 的引入,此上下文中使用的关键字已被删除。
Formerly, Set was used to indicate that an object reference was being assigned (Let was the default). Because default properties no longer are supported unless they accept parameters, these statements have been removed.
以前, Set 用于指示正在分配对象引用(Let 是默认值)。因为不再支持默认属性,除非它们接受参数,所以这些语句已被删除。
Module Module1
Sub Main()
Dim person As New Person("Peter")
Dim people As New People()
people.Add(person)
'Use the default property, provided we have a parameter'
Dim p = people("Peter")
End Sub
End Module
Public Class People
Private _people As New Dictionary(Of String, Person)
Public Sub Add(ByVal person As Person)
_people.Add(person.Name, person)
End Sub
Default Public ReadOnly Property Person(ByVal name As String) As Person
Get
Return _people(name)
End Get
End Property
End Class
Public Class Person
Private _name As String
Public Sub New(ByVal name As String)
_name = name
End Sub
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
End Class
回答by Joel Coehoorn
Some things to remember for .Net:
.Net 需要记住的一些事情:
- NEVER use Server.CreateObject() in .Net code. Ever.
- NEVER Dim a variable without giving it an explicit type. Except for new
Option Infer
linq types - NEVER use the Set keyword. Except when defining a property.
- 切勿在 .Net 代码中使用 Server.CreateObject()。曾经。
- 永远不要在没有给它一个明确的类型的情况下给一个变量变暗。除了新的
Option Infer
linq 类型 - 切勿使用 Set 关键字。定义属性时除外。
In fact, in .Net you can get rid probably of the CDONTS dependancy entirely, as .Net has a built-in mail support:
事实上,在 .Net 中你可以完全摆脱 CDONTS 的依赖,因为 .Net 有一个内置的邮件支持:
Dim smtp As New System.Net.SmtpClient()
Dim message As New System.Net.MailMessage(EmailFrom, EmailTo, Subject, Body)
smtp.Send(message)