8

When adding a new item (class, control, etc) in C# it will automatically add a namespace to the file depending on the location in the project.

Is this also available for VB.NET?

The code 'Namespace DataClasses.AX' and 'End Namespace' would be generated.

Namespace DataClasses.AX
    <Serializable()> _
    Public Class AxInventItem

#Region " Constructors "
        Sub New()

        End Sub
#EndRegion
    End Class
End Namespace
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87

4 Answers4

14

You dont say what version of visual studio you are using, but it is doable by default with VS2008.

Goto : C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\VisualBasic\Code\1033\Class.zip (if this doesnt exist i will post you the contents of mine).

Inside you will find two files: Class.vb and Class.vstemplate

Open Class.vb (i extracted it and edited in notepad++ but anything will do). Change it to read:

Namespace $itemfolder$

    Public Class $safeitemname$

    End Class

End Namespace

Shut Visual stuid (all instances)

Save and replace the one inside the zip.

Openup a command prompt (easiest is to use the visual studio command prompt), and run the following:

devenv.exe /InstallVSTemplates

Open Visual Studio again, create a blank project and add a folder to the project, and add a class inside the folder and you will have your namespace automagically added to your class file.

Edit

Class.vb:

Namespace $itemfolder$

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks>Class Created by $username$ on $date$</remarks>
    Public Class $safeitemname$

    End Class

End Namespace

Class.vstemplate:

<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name Package="{164B10B9-B200-11D0-8C61-00A0C91E29D5}" ID="3020" />
    <Description Package="{164B10B9-B200-11D0-8C61-00A0C91E29D5}" ID="3021" />
    <Icon Package="{164B10B9-B200-11D0-8C61-00A0C91E29D5}" ID="4510" />
    <ProjectType>VisualBasic</ProjectType>
    <SortOrder>100</SortOrder>
    <DefaultName>Class.vb</DefaultName>
    <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
    <TemplateID>Microsoft.VisualBasic.Code.Class</TemplateID>
    <RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>
  </TemplateData>
  <TemplateContent>
    <ProjectItem ReplaceParameters="true">Class.vb</ProjectItem>
  </TemplateContent>
  <WizardExtension>
    <Assembly>VBClassTemplateWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=f12a64f29cf5aee5</Assembly>
    <FullClassName>VBClassTemplates.VBClassReplacements</FullClassName>
  </WizardExtension>

</VSTemplate>
Pondidum
  • 11,457
  • 8
  • 50
  • 69
  • I'm using VS2008. The templete parameter $itemfolder$ isn't found, but you made me look in the right direction I'm using $rootnamespace$ now like C# does. Only C# also add's the folders as part of the namespace. – Ralf de Kleine Jul 23 '10 at 11:22
  • Could you post the content of the Class.vb and Class.vstemplate? – Ralf de Kleine Jul 23 '10 at 11:28
  • 1
    I hate to necro-post, but, if I'm not mistaken adding `Namespace $rootnamespace$` is a bad idea since in VB.Net the root namespace is automatically added to everything. You will end up with `MyProjectNS.MyProjectNS.MyClass` – MHollis Jun 23 '11 at 15:27
  • @MHillis: `$rootnamespace$` takes your project's root namespace into consideration. – Pondidum Jun 24 '11 at 08:10
  • Microsoft Visual Studio --------------------------- Error: this template attempted to load an untrusted component 'VBClassTemplateWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=f12a64f29cf5aee5'. For more information on this problem and how to enable this template, please see documentation on Customizing Project Templates. Better no use VB.NET! – antonio Jan 18 '18 at 02:46
1

This is available as part of Resharper where you can create templates to do this.

This is the standard template.

Namespace $NAMESPACE$
    Public Class $CLASS$ 
        $END$
    End Class
End Namespace
chrissie1
  • 5,014
  • 3
  • 26
  • 26
0

And I guess you could use a T4 template to do the same thing, or adapt the one that is there. But T4 is not very open yet. AS far as I know.

chrissie1
  • 5,014
  • 3
  • 26
  • 26
-1

Visual Studio 2012: In the project properties in the application tab there is a field named default namespace. This is automatically added to the files inside the project.

So when you want to reference something in a different project you can import the namespace that is defined there. So there is basically no need to manipulate any template, I guess.

Marcell
  • 809
  • 8
  • 23