Question : passing in null for the function

Hi,

Have a look at the following line of code please:

PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);

When this function being called they are passing a null for the transaction parameter and casting that with the sqltransaction type?

This is the function definition:

private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
{
}
From this I got two questions some need to explain to me please.

(1)When the function expecting a transaction type object you pass in a null value.
(2) Also they are casting that null value with sqltransaction type, can you do that in .net?
Any help would grately appreciated.

Answer : passing in null for the function

Hello, ube100,

wrt question #2:
 (afaik) you can cast null to any type you like.  It is still null.  That is, I believe the casting is unnecessary, but probably does no harm.

wrt question #1:
In general, whether a function can accept null as a value (and what it does with a null value) is up to the function.  Sorry, but I don't know the details for the PrepareCommand function.  However, I would guess  that passing in a null for transaction would mean that the command would NOT be executed as part of a transaction.

Cheers,
Randy
Random Solutions  
 
programming4us programming4us