Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / XML

Creating project item code templates for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.86/5 (47 votes)
26 Jul 200517 min read 186.3K   2.5K   142  
This article describes how new project item code templates work in Visual Studio .NET 2003 (and 2005) and presents a component that makes it easy to write new templates.
[%
<wizard>
	<page>
		<title>Select data type</title>
		<description>Select data type for which to create a strongly typed collection</description>
		<variable name="datatype"/>
	</page>	
</wizard>
%]Imports System
Imports System.Collections

Public Class [%<variable type="standard" name="filenamebase"/>%]
    Inherits CollectionBase

    Default Public Property Item(index As Integer) As [%<variable type="custom" name="datatype"/>%]
        Get
            Return CType(List(index), [%<variable type="custom" name="datatype"/>%])
        End Get
        Set
            List(index) = value
        End Set
    End Property

    Public Function Add(value As [%<variable type="custom" name="datatype"/>%]) As Integer
        Return List.Add(value)
    End Function 'Add

    Public Function IndexOf(value As [%<variable type="custom" name="datatype"/>%]) As Integer
        Return List.IndexOf(value)
    End Function 'IndexOf

    Public Sub Insert(index As Integer, value As [%<variable type="custom" name="datatype"/>%])
        List.Insert(index, value)
    End Sub 'Insert

    Public Sub Remove(value As [%<variable type="custom" name="datatype"/>%])
        List.Remove(value)
    End Sub 'Remove

    Public Function Contains(value As [%<variable type="custom" name="datatype"/>%]) As Boolean
        ' If value is not of type [%<variable type="custom" name="datatype"/>%], this will return false.
        Return List.Contains(value)
    End Function 'Contains


   Protected Overrides Sub OnInsert(index As Integer, value As [Object])
      ' Insert additional code to be run only when inserting values.
   End Sub 'OnInsert


   Protected Overrides Sub OnRemove(index As Integer, value As [Object])
      ' Insert additional code to be run only when removing values.
   End Sub 'OnRemove


   Protected Overrides Sub OnSet(index As Integer, oldValue As [Object], newValue As [Object])
      ' Insert additional code to be run only when setting values.
   End Sub 'OnSet


   Protected Overrides Sub OnValidate(value As [Object])
	  ' This check is commented out since it doesn't work for all types
      'If Not value.GetType() Is Type.GetType("[%<variable type="custom" name="datatype"/>%]") Then
      '   Throw New ArgumentException("value must be of type [%<variable type="custom" name="datatype"/>%].", "value")
      'End If
   End Sub 'OnValidate


End Class '[%<variable type="custom" name="datatype"/>%]Collection

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
Worked in the industry since 1996, as a developer, architect, project leader, etc.

Current interests include DirectX, Visual Studio adaptation (add-ins, macros, etc), C# and C++.

Comments and Discussions