Parameters by reference

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need help/ example with this kind of probem:

public static Boolean Look (object sender, SqlConnection Con, string Table,
object[] Columns);
While calling this method, the Columns parameters will consist both value
types (ex: int) and reference types (textBox)

Ex :

string MyStr;

int k;

Look (sender, Conn, "MyTable", new object [] {textBox1, MyStr, k})

I want MyStr and k variables to be passed by reference (not by value) , but
when I define the method as:

public static Boolean Look (object sender, SqlConnection Con, string Table,
ref object[] Columns);

I got the error message:

cannot convert object [] to ref object[]

while calling the function.

Any help?

Thanks
 
I want MyStr and k variables to be passed by reference (not by value) , but
when I define the method as:

public static Boolean Look (object sender, SqlConnection Con, string Table,
ref object[] Columns);

I got the error message:

cannot convert object [] to ref object[]

while calling the function.

Any help?

Yes - you need to specify ref in the calling line, as well as the
method declaration.

See http://www.pobox.com/~skeet/csharp/parameters.html
 
Yes - you need to specify ref in the calling line, as well as the
method declaration.

Thanks, but when I try:

Look (sender, Conn, "MyTable", new object [] {textBox1, MyStr, ref k})

I got an error

; expected
 
Yes - you need to specify ref in the calling line, as well as the
method declaration.

Thanks, but when I try:

Look (sender, Conn, "MyTable", new object [] {textBox1, MyStr, ref k})

I got an error

; expected

Well that's because you haven't got a semi-colon, but that's a
different matter. (The error message is very clear, however - you
should be able to work out that when it says "; expected" it means
you're missing a semi-colon.)

However, you can't create an array with elements in which *are* "passed
by reference" - ref is only applicable to parameters themselves, and
here k is *not* a parameter, it's just part of the array initializer.
 
You need to pass the ref key in the Calling method aswell... That should
solve your problem...

ie.
Look (sender, Conn, "MyTable", ref new object [] {textBox1, MyStr, k})
I need help/ example with this kind of probem:

public static Boolean Look (object sender, SqlConnection Con, string Table,
object[] Columns);
While calling this method, the Columns parameters will consist both value
types (ex: int) and reference types (textBox)

Ex :

string MyStr;

int k;

Look (sender, Conn, "MyTable", new object [] {textBox1, MyStr, k})

I want MyStr and k variables to be passed by reference (not by value) , but
when I define the method as:

public static Boolean Look (object sender, SqlConnection Con, string Table,
ref object[] Columns);

I got the error message:

cannot convert object [] to ref object[]

while calling the function.

Any help?

Thanks
 
Dilip Krishnan said:
You need to pass the ref key in the Calling method aswell... That should
solve your problem...

ie.
Look (sender, Conn, "MyTable", ref new object [] {textBox1, MyStr, k})

That won't quite work - a ref parameter has to be a variable,
basically.
 
Back
Top