Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

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


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;
        }

 <cty0@gmail.com> wrote:
> 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.

--
Jon Skeet - <s@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

-----------------------------------------------Reply-----------------------------------------------

On 6 7 ,   3 31 , Jon Skeet [C# MVP] <s@pobox.com> wrote:

A

-----------------------------------------------Reply-----------------------------------------------
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 :=)

-----------------------------------------------Reply-----------------------------------------------

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";

}
<cty0@gmail.com> wrote in message

news:1181201169.743438.325900@o11g2000prd.googlegroups.com...

On Jun 7, 8:26 am, cty0@gmail.com wrote:

> 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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc