using statement with comma

  • Thread starter Thread starter David Parker
  • Start date Start date
D

David Parker

The below seems to work...
using(dbconn=DatabaseHelper.CreateConnection("David_Projects"))
{
using(dbcmd=GetSQLSearchCommand(dbconn))
{
// code here
}
}


However, this version doesn't. Any ideas?
using((dbconn=DatabaseHelper.CreateConnection("David_Projects")),(dbcmd=GetS
QLSearchCommand(dbconn)))
{
// code here
}

Visual Studio .NET underlines the , in red and complains "Invalid expression
term ','".
 
Why should this work?

It is the same as

while(statement)
{
while(statement)
{
}
}

compared to

while((statement), (statement))
{
}
 
In the documentation there is the following example of the using
statement...
using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial",
10.0f))

I'm guessing the problem is because I'm trying to use two different types
(SqlConnection and SqlCommand) instead of just one (as in the Font example).
 
Ah, in that case it would be more like

for(int i = 0, j = 1;;)

Only one type declaration is allowed, i and j must be of the same type.
 
From the C# specification:

When a resource-acquisition takes the form of a local-variable-declaration,
it is possible to acquire multiple resources of a given type.
So you are correct; the type of the resources being acquired in the using
statement must be the same.HTHDalePresMCAD, MCDBA, MCSE"David Parker"
 
Back
Top