Importing a single function from a module in VB.Net

时间:2019-06-01 14:00:30

标签: .net windows vb.net winforms namespaces

I was wondering if I can import a single function like python without importing all the other things like python does:

from prog1 import func

1 个答案:

答案 0 :(得分:1)

In VB you need to do two things to able to use a function or anything else declared in another assembly or namespace.

  1. Add a reference to the assembly (*.dll, *.exe).
  2. Import the namespace. E.g. Imports System.Drawing.

Then you can use all the things in there.

Instead of using an Imports statement you can also specify the namespace directly. Example:

'Without Imports
Dim image As System.Drawing.Image
image = New System.Drawing.Bitmap(myImagefile)

With Imports System.Drawing at the top of the code:

Dim image As Image
image = New Bitmap(myImagefile)

The most important assemblies are already referenced and the most often used namespaces imported implicitly. These namespaces are list in the project properties under References.

Note that you cannot import single functions, you always have to reference a whole assembly. The Imports does not influence the resources used. It only makes it handier to access things. They reduce typing.

Note also, that the standard .NET libraries of the .NET Framework are installed on the system and are already compiled. They are not imported physically into your project, they are only referenced. I.e., using them does not make your application bigger.