how to change variable value which is using as param list in sub functoin

C

cty0000

I need to set several variable (can be different type) by sub
function..
I don't know the count of variable and type, so i used object type and
parameter list like below source..
After setData function call a1,a2 is still -1 and "AAA"
I want to be change as 3,"BBB"
Does anybody can help me???

class Program
{
static void Main(string[] args)
{
int a1 = -1;
string a2 = "AAA";
setData(a1,a2);
}

public void setData(params object[] list)
{
list[0] = 3;
list[1] = "BBB;
}
}
 
J

Jon Skeet [C# MVP]

I need to set several variable (can be different type) by sub
function..
I don't know the count of variable and type, so i used object type and
parameter list like below source..
After setData function call a1,a2 is still -1 and "AAA"
I want to be change as 3,"BBB"
Does anybody can help me???

You need to pass the parameters by reference (the ref modifier).
However, you can't do that with parameter arrays (the params keyword).

See http://pobox.com/~skeet/csharp/parameters.html for more
information.
 
C

cty0000

You need to pass the parameters by reference (the ref modifier).
However, you can't do that with parameter arrays (the params keyword).

Seehttp://pobox.com/~skeet/csharp/parameters.htmlfor more
information.

A
 
C

cty0000

Thanks for your help..

Actually what i want to do is that communication between two class.
One is sending message with datas (serveral message)
The other is catching message and set variable with included message
from sender...

According to http://pobox.com/~skeet/csharp/parameters.html
Parameter arrays allow only variable number of arguments but I need
reference argument...
It there any other solution???

Please help me :=)
 
M

Marc Gravell

A params array is (broadly) syntactic sugar for a regular array, and
since arrays are reference types, if contents are edited in place the
values will remain edited to the caller; "ref" here would just allow
you to *reassign* the array, which isn't what you want.

Unfortunately you need to use regular array syntax to make this work,
but it can be done as below. However, my advice is to represent the
values in a class; this gives you far better context, i.e. you don't
have to remember that index 8 is the forename and should be a string -
you have a "string Forename" property.

object[] values = {3, "abc"};
SomeFunc(values);
object arg0 = values[0]; // will be 7
object arg1 = values[1]; // will be "Fred"

....
private void SomeFunc(object[] args) {// could be "params", but
doesn't help any
args[0] = 7;
args[1] = "Fred";
}
 
J

Jon Skeet [C# MVP]

Actually what i want to do is that communication between two class.
One is sending message with datas (serveral message)
The other is catching message and set variable with included message
from sender...

I can't say I like this as a design, personally. I think it would be
much more straightforward to encapsulate the parameters either into a
mutable type, or let the method take one set of parameters and return
its result in a return value (either of the same type or a different
one).

Reference parameters are generally harder to read and use, IMO.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top