Hi,
In the follow program I would like to have the parameter "c" in method
"doSomething" as a value parameter, but it's running as a reference
parameter. Why? How can I get parameter "c" as a value (or clone,
copy)?
Current result:
value1
value2
Expected result:
value1
class Program
{
static void Main(string[] args)
{
SqlCommand sqlCommandSelect = new SqlCommand();
sqlCommandSelect.Parameters.AddWithValue("@col1",
"value1");
doSomething(sqlCommandSelect.Parameters);
foreach (SqlParameter param in
sqlCommandSelect.Parameters)
{
Console.WriteLine(param.Value);
}
Console.ReadLine();
}
private static void doSomething(SqlParameterCollection c)
{
c.AddWithValue("@col2", "value2");
//I want to use the new value only here
}
} 4 1767
Everton,
SqlParameterCollection is a reference type. The parameter itself can
not be changed, but the parameter is a reference. You can still modify what
the refrerence points to.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Everton Berz" <ev**********@gmail.comwrote in message
news:11*********************@k35g2000prh.googlegro ups.com...
Hi,
In the follow program I would like to have the parameter "c" in method
"doSomething" as a value parameter, but it's running as a reference
parameter. Why? How can I get parameter "c" as a value (or clone,
copy)?
Current result:
value1
value2
Expected result:
value1
class Program
{
static void Main(string[] args)
{
SqlCommand sqlCommandSelect = new SqlCommand();
sqlCommandSelect.Parameters.AddWithValue("@col1",
"value1");
doSomething(sqlCommandSelect.Parameters);
foreach (SqlParameter param in
sqlCommandSelect.Parameters)
{
Console.WriteLine(param.Value);
}
Console.ReadLine();
}
private static void doSomething(SqlParameterCollection c)
{
c.AddWithValue("@col2", "value2");
//I want to use the new value only here
}
}
Hi,
--
Ignacio Machin www.laceupsolutions.com
Mobile & warehouse Solutions.
"Everton Berz" <ev**********@gmail.comwrote in message
news:11*********************@k35g2000prh.googlegro ups.com...
Hi,
In the follow program I would like to have the parameter "c" in method
"doSomething" as a value parameter, but it's running as a reference
parameter. Why? How can I get parameter "c" as a value (or clone,
copy)?
Take a look at http://www.yoda.arachsys.com/csharp/parameters.html it's a
very good explanation about parameters in C#
Everton Berz wrote:
In the follow program I would like to have the parameter "c" in method
"doSomething" as a value parameter, but it's running as a reference
parameter. Why? How can I get parameter "c" as a value (or clone,
copy)?
As Nicholas says, you can't. You'd have to clone it explicitly before
passing it, as the type is a reference type and so a reference is always
passed. It's passed by value, but it's still a reference.
This (or at least, the more general topic) is a common question and Jon
Skeet's web site includes an article that should help: http://www.pobox.com/~skeet/csharp/parameters.html
Pete
Thank you for answer.
I really did not know that SqlParameterCollection is a reference
type.
There is no Clone method in this class, I think I need to clone my
SqlCommand object.
--
Everton
On Oct 12, 7:15 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
Everton Berz wrote:
In the follow program I would like to have the parameter "c" in method
"doSomething" as a value parameter, but it's running as a reference
parameter. Why? How can I get parameter "c" as a value (or clone,
copy)?
As Nicholas says, you can't. You'd have to clone it explicitly before
passing it, as the type is a reference type and so a reference is always
passed. It's passed by value, but it's still a reference.
This (or at least, the more general topic) is a common question and Jon
Skeet's web site includes an article that should help:http://www.pobox.com/~skeet/csharp/parameters.html
Pete
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dmitry |
last post by:
Hi,
I have defined interface for COM components which inludes an argument being
filled with additional error info, if such occurs. If inside I raise COM
Error, I populate that parameter.
In COM...
|
by: Dr Duck |
last post by:
GDay all,
Something seems odd to me....
I wrote a simple C# function
public void bind(ref object a, ref object b, bool atob)
{
if(atob)
b = a;
else
|
by: Bob Maggio |
last post by:
I have created a function that returns a string containing raw HTML to
be used on a web form (see below):
private string GetReportHTML()
{
string strHTML = "";
// I have some code here that...
|
by: Tee |
last post by:
Hi guys,
I am confused about the usage of "ref".
Please see this code:
private void button3_Click(object sender, System.EventArgs e)
{
|
by: Cmtk Software |
last post by:
The following code:
public __gc class MyClass
{
public:
void MyFunc (int __gc* number);
};
Generates the following metadata for C# when compiled in VC 2005 with the
/clr:oldsyntax switch...
|
by: not_a_commie |
last post by:
A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |