What did VB replace the function “Set” with?

2020-02-12 08:03发布

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?

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 set
3条回答
姐就是有狂的资本
2楼-- · 2020-02-12 08:27

Set is a keyword in VB6, with the intrudction of VB.NET the keyword, as used in this context, was removed.

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.

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
查看更多
Deceive 欺骗
3楼-- · 2020-02-12 08:40

Some things to remember for .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.

In fact, in .Net you can get rid probably of the CDONTS dependancy entirely, as .Net has a built-in mail support:

Dim smtp As New System.Net.SmtpClient()
Dim message As New System.Net.MailMessage(EmailFrom, EmailTo, Subject, Body)
smtp.Send(message)
查看更多
一纸荒年 Trace。
4楼-- · 2020-02-12 08:43

If you mean the VB6 syntax

Set obj = new Object

then you can simply remove the Set

obj = new Object()
查看更多
登录 后发表回答