Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.
2009-04-12
Completion lists is one of the nice features that you can use in Visual Basic 9 to help programmers to initialize objects. In a way, this works like enumerations, but enumerations are really named integer values, and a completion list can consist of custom types. To create a completion list, just make an extra class that consists of the templates that you want to have in your completion list. Each item (implemented as a member in the extra class) in the list is represented by a static (shared in VB) object of the same type as the object that it will be able to initialize. For this example I will create a completion list for my custom point class.
Public Class Point Private m_X As Integer Private m_Y As Integer Public Sub New(ByVal X As Integer, ByVal Y As Integer) Me.m_X = X Me.m_Y = Y End Sub Public Overrides Function ToString() As String Return Me.m_X.ToString() & " * " & Me.m_Y.ToString() End Function End Class
This is the completion list (referred to earlier as the extra class):
Public NotInheritable Class MyListOfTemplates Public Shared ReadOnly Property Spaghetti() As Point Get 'Initialize and return one template item. Return New Point(10, 10) End Get End Property Public Shared ReadOnly Property Strawberry() As Point Get 'Initialize and return one template item. Return New Point(20, 20) End Get End Property Public Shared ReadOnly Property Darwin() As Point Get 'Initialize and return one template item. Return New Point(150, 200) End Get End Property End Class
At this point, I can create a point from a template, like this:
Dim P As Point = MyListOfTemplates.Spaghetti
I already have the enumeration functionality, but no special help from the code editor. This picture shows what I can expect:
To make the magic happen, connect the point class to the template list, using a remark containing the name of the template list. The remark must start with three apostrophes, like this:
''' <completionlist cref="MyListOfTemplates"/> Public Class Point
Now, the code editor knows what to suggest when you declare a new point, as shown here.
Categories: Visual Basic 9
Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!
Leave a Reply