Null in Visual Basic .Net???

  • Thread starter Familjen Karlsson
  • Start date
F

Familjen Karlsson

How will the code under look like in Visual Basic .Net, since you can't use
the keyword null anymoore in Visual Basic .Net. What can I use instead.

SqlConnection connection = null; SqlCommand command = null; SqlDataReader
reader = null;

String connectionString = "integrated security=SSPI;"; connectionString +=
"data source=(local);"; connectionString += "initial catalog=Northwind";

String sqlCommandString = "SELECT CustomerID, CompanyName FROM Customers";

try {

connection = new SqlConnection(connectionString);
command = new SqlCommand(sqlCommandString, connection); connection.Open();
reader = command.ExecuteReader();
if(reader != null)
{
while(reader.Read())
{
Console.Write(reader["CustomerID"]+ "\t");
Console.WriteLine(reader["CompanyName"]); ...

}

}

}catch( ){....}
finally
{
if(reader != null)
reader.Close();
if(connection.State == ConnectionState.Open)
connection.Close();
}

mvh

Fia
 
H

Herfried K. Wagner [MVP]

Familjen Karlsson said:
How will the code under look like in Visual Basic .Net, since you can't
use
the keyword null anymoore in Visual Basic .Net. What can I use instead.
SqlConnection connection = null; SqlCommand command = null; SqlDataReader
reader = null;
[...]
reader = command.ExecuteReader();
if(reader != null)

Visual Basic .NET initializes local variables to 'Nothing' ("null
reference") automatically. Thus you do not need to assign 'Nothing' to
these variables explicitly. The line above can be written in VB.NET as
shown below:

\\\
If Not Reader Is Nothing Then
...
End If
///

VB 2005 supports the 'IsNot' operator, which can be used to shorten the code
to 'If Reader IsNot Nothing Then...'.
 
C

Cor Ligthert [MVP]

Familjen,

You have to distinct a database null value which is a "dbnull.value" and an
object which has nothing as address (not instanced) and therefore not
referancable, which is than "IS Nothing",

Because that address was classical a null value in hardware, is in older
types of programming direct that hardware word "null" used for that.

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top