views:

286

answers:

2

I have a function within a class which I would like to return the class itself however "return this" appears to be invalid in VB.

I am using ASP.NET v1.1 if that makes a difference?

Sample (extremely simplified) code is as follows:

Public Class Cart

    Private sItems As String

    Public Function addItem(ByVal itemName As String) As Cart
        sItems = sItems + "|" + itemName + "|"
        Return THIS
    End Function

End Class

Any help would be greatly appreciated. Thanks very much.

+6  A: 

In VB Me is the keyword, for the class you're in, so try

Public Class Cart
    Private sItems As String

    Public Function addItem(ByVal itemName As String) As Cart
        sItems = sItems + "|" + itemName + "|"        
        Return Me
    End Function

End Class
MrTelly
+3  A: 

just a little pedantic statement

You are returning an instance of the type of Class, you are not returning the class type itself.

this and Me refer to the current instance, or object, rather than the class definition which would be this.GetType()

Alan Mullett
+1 cause I'm an incurable pedant too :)
Binary Worrier
To be pedantic, it's "pedantic" rather than "pendantic" :-)
RoadWarrior
lol @ RoadWarrior :)editied.
Alan Mullett