Windows Develop Bookmark and Share   
 index > Windows Forms General > difference between as and =
 

difference between as and =

Hello can sombody explain me the difference between as and = opperator in the following sample:

 * Dim insert_log = New SqlClient.SqlCommand("spINSERT_log", Conn_log)
 
 * Dim insert_log As New SqlClient.SqlCommand("spINSERT_log", Conn_log)

What is the difference between "= New"  and "As New"
MigrationUser 1  Friday, January 07, 2005 9:18 AM
In the first line, the variable will be of type object, in the second line of SqlCommand. The first line probably won't even compile with Option Strict On (there's no explicit type declared), and you don't get IntelliSense for insert_log later.
MigrationUser 1  Monday, January 10, 2005 2:06 AM
thanks for your help !!
MigrationUser 1  Monday, January 10, 2005 4:06 AM
Keep in mind that "As" is not an operator.

This gets a little confusing becuase of the syntax shortcuts allowed by VS.Net.  As stated above, the second line is the one to use because it makes "insert_log" a recognized SqlClient.SqlCommand object, rather than just a generic object.  It might however be helpful to see that this is actually two lines of code which have been combined:

Dim insert_log as SqlClient.SqlCommand
insert_log = New SqlClient.SqlCommand("spINSERT_log", Conn_log)

You would really only want to use the two individual lines if they did not reside in the same procedure.  You might declare your object at the class level so that it is available to other procedures, and only instantiate it in a procedure when needed.
MigrationUser 1  Tuesday, January 11, 2005 2:56 PM
is there any difference between:

Dim insert_log As New SqlCommand("spINSERT_log", Conn_log) 

and

Dim insert_log As SqlCommand =  New SqlCommand("spINSERT_log", Conn_log) 


?
MigrationUser 1  Tuesday, January 11, 2005 5:02 PM
As far as I can tell, no there is really no difference other than the length of the line of code.  I've seen professional examples both ways; it seems to be author preference.  I also haven't seen any perfomance info indicating that one way is perferable to the other.
MigrationUser 1  Wednesday, January 12, 2005 11:25 AM
Only thing I can think of is if you wanted polymorphism
Dim pic as Image = new Bitmap(10, 10)
MigrationUser 1  Thursday, January 13, 2005 4:05 PM

You can use google to search for other answers

Custom Search

More Threads

• cut copy paste using graphics path and clipboard+C#..
• reorder listview based on Tag
• disable double click event on ListViewItem
• How to custom DataGridView
• little forms in my panel
• To display Node atrributes such that node name added to tree?
• invoking events on controls
• Creating a login form
• How to draw arrowhead lines?
• how to solve Cross Thread?.