Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I don't know about Imports in vb.net.

Why do we used import like Imports System.data?

Please guide me, is there any free site for downloading ebooks for vb.net?


thank you

harshit
Posted
Updated 25-Oct-11 21:45pm
v2
Comments
Dalek Dave 26-Oct-11 3:45am    
Edited for Grammar and Spelling.

Using the Imports key word at the top of your document enables you to not have to fully type out the namespace everytime you use it. It's the same in C#, except VB has some namespaces imported for you as a standard, whereas C# always needs everything you need imported (using C#)
So this code:
VB
Public Class Test

   Public Sub New
      Dim cmb As New System.Data.SqlClient.SqlCommand
   End Sub

End Class

Could be a lot shorter like this:
VB
Imports System.Data.SqlClient
Public Class Test

   Public Sub New
      ' Namespace is imported so we don't have to fully type the namespace!
      Dim cmb As New SqlCommand
   End Sub

End Class

You always have to Import the entire namespace.
So this won't work:
VB
Imports System
Imports Data.SqlClient ' This still needs to be System.Data.EntityClient

You can also define aliases for namespace, like so:
VB
Imports MyAlias = System.Data.SqlClient

More on Imports here[^]
 
Share this answer
 
v2
Comments
Abhinav S 26-Oct-11 2:42am    
My 5. Amazing how we end up with the same links!
Sander Rossel 26-Oct-11 3:26am    
Thanks! :)
An import statement is like a shortcut that allows you to use types in a reference directly.

Example - you add a dll reference to System.Data.
Now when you use the DataTable class, you will need to write System.Data.DataTable to use it.

If you add Imports System.Data at the top of the class file, you can easily use DataTable across your class without writing System,Data. whenever you use it.

Read more about imports here[^].
 
Share this answer
 
Comments
Sander Rossel 26-Oct-11 3:25am    
My 5 to you too. Not only the same link (I guess we both know how to use MSDN/Google ;p ), but also partially the same Namespace! Could've picked anything! ;)
Have a look at below link for information on "Imports".
Imports Statement

Have a look at below link to start with VB.Net.
Visual Basic .NET Programming for Beginners


Internet Search Engine is one of the best media to find Tutorials on any topic.

Google Search Result for VB.Net Tutorial

Bing Search Result for VB.Net Tutorial
 
Share this answer
 
Comments
Abhinav S 26-Oct-11 3:25am    
5!
RaisKazi 26-Oct-11 3:32am    
Thank you Abhinav. :)
An Import Statement is mainly used to link with the external dynamic link libraries i.e(Dll's)
or the dll's provided by the .Net Framework so we can
access the additional functionalities defined in the dll's in our project.
Dll contains the classes.we can use the subrutins and functions define in
class by creating object of that class

For Example :
To perform Database operations we have to import
VB
Imports System.Data
or
To perform different operations on file we have to import
VB
Imports System.Io


To Create object of class :
VB
Dim Obj as New ClassName


To Use Functions or Methods/Subrutines
VB
Obj.[Function/Method]
 
Share this answer
 
v4
"import.system.anything" is very important for importing something from the FCL library of .net...as we know that FCL is library where all the class are present
and thus we can use its method,properties etc,,,
Forexample if we want to use the image in a progran then we should import the image class....etc
But addrefernce is also necessay with import if we are doing program in console.while in form programing only import works all....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900