VB.NET, Visual Studio and Namespaces

Asked

Viewed 149 times

0

How to make Visual Studio 2017 + VB.NET create new classes already with namespace, as it does in C#?

I’ve tried creating and editing item templates using the parameter $itemfolder$, but it doesn’t seem to work:

Template

Namespace $itemfolder$

    Public Class $safeitemrootname$

    End Class

End Namespace

Exit

Namespace $itemfolder$

    Public Class MyClass2

    End Class

End Namespace

Already using the parameter $rootnamespace$, does not work as it should:

Template

Namespace $itemfolder$

    Public Class $safeitemrootname$

    End Class

End Namespace

Exit

Namespace ConsoleApp1

    Public Class MyClass

    End Class

End Namespace

Solution Explorer

I was hoping the namespace was ConsoleApp1.TestNamespace or TestNamespace.

Have some way to get VS2017 to automatically add namespaces to new VB.NET classes?

I know Resharper works, but he gets paid.

1 answer

1

No, this is not possible. Visual Basic does not support the concept of creating namespaces same as C#, for you to create a project "with several standard namespaces" you should first remove the namespace design standard.

inserir a descrição da imagem aqui

Where is written "Your Application" you leave empty, without writing anything. Unfortunately it is something very boring to do.

And to create a file, just type the namespace in the archive itself:

Namespace System
    Public Class ConsoleClone
        Public Shared Sub WriteLine(ByVal text$)
             Console.WriteLine(text)
        End Sub
    End Class
End Namespace

And you will call as:

System.ConsoleClone.WriteLine("Olá, mundo!")

Not to be ignorant, I’ve been trying to find a way to do this for a long time, and I haven’t found one. I don’t even know if it is possible to do this, all my programming time with Visual Basic turned me this way, and it is not as laborious as expected.

  • The focus was on VS automatically filling the namespace in class creation as in C#. It could be with $itemfolder$, that would look something like Project Name.Pasture1.Pasture2 or Pasture1.Pasture2 (in case VB.NET treats namespaces differently than C#). In tests, VS2013 worked these Item templates.

  • I tried this on VS2017, it did not work, it gave syntax error: http://prntscr.com/fv881h

Browser other questions tagged

You are not signed in. Login or sign up in order to post.