all groups > dotnet clr > august 2005 >
You're in the

dotnet clr

group:

How to cast a Hashtable to a SqlParameterCollection in C#?


How to cast a Hashtable to a SqlParameterCollection in C#? Jaime Stuardo
8/30/2005 10:29:02 AM
dotnet clr:
Hi all....

Is it possible to cast a Hashtable object to a SqlParameterCollection object?

Both types implements ICollection interface, so I think it is possible to do
that without the need of using foreach and copying each element individually.

I tried
SqlParameterCollection sqlParamIn =
(SqlParameterCollection)(ICollection)dicParamIn;

But it throws a runtime exception.

Any help would be greatly appreciated. Thanks
Re: How to cast a Hashtable to a SqlParameterCollection in C#? David Browne
8/30/2005 1:15:07 PM

[quoted text, click to view]

Copy, don't cast.

foreach (SqlParameter p in dicParamIn)
{
cmd.Parameters.Add(p);
}

David

Re: How to cast a Hashtable to a SqlParameterCollection in C#? Jon Skeet [C# MVP]
8/30/2005 7:21:23 PM
[quoted text, click to view]

No.

[quoted text, click to view]

No, it's not. Just because they both implement the same interface
doesn't mean anything in terms of treating one as if it's the other.
You wouldn't expect to be able to cast from DataTable to FileStream,
just because both of them implement IDisposable, would you?

I doubt that copying all the parameters is actually likely to be a
significant performance problem, and you could encapsulate the logic in
a single method.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button