What did VB replace the function “Set” with?

前端 未结 3 1687
慢半拍i
慢半拍i 2020-12-19 02:43

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 lon

相关标签:
3条回答
  • 2020-12-19 03:21

    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)
    
    0 讨论(0)
  • 2020-12-19 03:22

    If you mean the VB6 syntax

    Set obj = new Object
    

    then you can simply remove the Set

    obj = new Object()
    
    0 讨论(0)
  • 2020-12-19 03:26

    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
    
    0 讨论(0)
提交回复
热议问题