Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top