tags:

views:

186

answers:

3

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?

+7  A: 

If you mean the VB6 syntax

Set obj = new Object

then you can simply remove the Set

obj = new Object()
ChaosPandion
This is what it looks like originally:' send email Dim mailSet mail = Server.CreateObject("CDONTS.NewMail") mail.To = EmailTomail.From = EmailFrommail.Subject = Subjectmail.Body = Bodymail.Send
So that would just be changed by removing the word "Set"?
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
Thanks A Bunch!!
+3  A: 

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
"Because default properties no longer are supported unless they accept parameters"...I can't figure out what this statement means.
Robert Harvey
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
@mjv nice concise explanation there, good job
Dan F
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
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
"unless they accept parameters" .. I believe this is talking about indexers.
stimpy77
@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
A: 

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