Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass IDbCommand to function

Status
Not open for further replies.

jgillin

Programmer
Oct 22, 2003
82
US
Hi,
I have a function in ASP.NET that takes an IDbCommand object as a parameter. Here's the code:

protected int GetRecordCount(IDbCommand m_Command){
int recCount=0;
m_Command.CommandText = GetSelectStatement(true);
recCount = (int)m_Command.ExecuteScalar();
return recCount;
}

if I add the keyword "ref" before the parameter I get a compilation error (saying the types don't match), but it works fine without the ref.
I wanted to pass-by-ref for performance reasons.
Can someone clue me in to why the compiler won't let me do this? THanks for any thoughts.

Jeff



 
I *think* that when you pass an object instance to a method, you are really just providing a reference to the object instance for the method to work with (not sending a "by val" copy). So, in short, you are already passing the parameter by ref so there is no real performance gain.

Also, it may have something to do with IDBCommand being an interface, and that the compiler requires a concrete type when passing by ref.

HTH (and ready to stand corrected)


David
[pipe]
 
Thanks for the reply.
From what I've been reading I believe that object references are passed by value by default. I think you are on the right track with it being an interface type. Then again, I'm no expert. I'll try to cast it to a SqlCommand object before passing it just to see if it works. Everything is working OK as is (without using ref), but just hoping to understand better.
Thanks,

Jeff
 
A quick google search on the subject got me this discussion
which may or may not clarify the issue. (there's an MSDN link in there too, that I did not read).

From what I got out of the thread, the important thing to remember is that primitive data types, like Int32, are by default passed by value whereas object references are passed by reference (you pass a reference to the pointer).

At any rate, I don't think that you would need to explicitly pass an IDBCommand by reference for performance reasons.

Regards



David
[pipe]
 
Thanks for the link Dragonwell.
I understand now.
The reference is passed by value, but your still passing a reference to the object and not a copy of the object.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top