9

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?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81

3 Answers3

16

If you mean the VB6 syntax

Set obj = new Object

then you can simply remove the Set

obj = new Object()
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
  • This is what it looks like originally: ' send email Dim mail Set mail = Server.CreateObject("CDONTS.NewMail") mail.To = EmailTo mail.From = EmailFrom mail.Subject = Subject mail.Body = Body mail.Send –  Sep 23 '09 at 23:04
  • So that would just be changed by removing the word "Set"? –  Sep 23 '09 at 23:07
  • Yes. In the old days you had to distinguish between assignment of objects and scalar types. Now everything is pretty much an object so they dumped the set keyword completely. I'm actually surprised they didn't leave it and have the compiler ignore it so the old code would compile without the change. – JohnFx Sep 23 '09 at 23:11
6

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
Rohan West
  • 9,262
  • 3
  • 37
  • 64
  • 1
    "Because default properties no longer are supported unless they accept parameters"...I can't figure out what this statement means. – Robert Harvey Sep 23 '09 at 22:47
  • 8
    Say you had an Person object with default property Name. You could then say HisName = MyPersonObject (and VB would understand that you mean MyPersonObject.Name. Now if you wanted to assign the MyPersonObject to HisName , you would have to user Set, ie. Set HisName = MyPersonObject. Now that default properties are not supported, the Set became redundant and was removed. – mjv Sep 23 '09 at 22:53
  • No, I think what Robert Harvey doesn't understand is the "unless they accept parameters" part. I'm baffled by that as well. – Gregory Higley Sep 23 '09 at 22:57
  • 2
    Wow, kudos for understanding the language at that depth. But the 'Set' keyword was not removed from the language; it was needed for member property setters, so they co-opted it for that. – Robert Harvey Sep 23 '09 at 23:01
  • "unless they accept parameters" .. I believe this is talking about indexers. – Jon Davis Sep 23 '09 at 23:10
  • @stimpy77 / @Gregory H: Yes, the "unless it accepts parameters" is because default properties haven't completely been banned, only the ones that do not require anything after their name. A typical default property is a collections of something. For example a Book may have default property that is Pages, a collection of Page objects. So you can write IndexPage1 = MyBook(176) and the presence of the indexer (176) parameter removes any ambiguity. – mjv Sep 23 '09 at 23:37
  • @mjv What happens when the default property returns another object with a default property? Does a simple assignment return only to the depth of the first default property? Or does it recurse until the innermost default property? – Zev Spitz Feb 07 '18 at 13:52
4

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)
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794