marshal-by-reference class

tks423

Member
Joined
Jul 5, 2003
Messages
5
Hi,

I want to pass a parameter from one windows form to another.

C#:
//this function is in Form2, where c is a global public variable
private void button1_Click(object sender, System.EventArgs e)
{
  c = (int) numericUpDown1.Value + 
             (int) numericUpDown2.Value;

   Form1.PrintToTextBox(this);

}

//this is the function in Form1
public static void PrintToTextBox(Form2 obj)
{
  string b;
  b = obj.c.ToString();		
}

This is the error I get: Cannot pass WindowsApplication1.Form2.c as ref or out, because WindowsApplication1.Form2.c is a marshal-by-reference class

Any help would be much appreciated,
-Tim
 
Last edited by a moderator:
Ah, yes. This is a good one. A more compact example of what youre experiencing is shown below:

[CS]
class Test : System.MarshalByRefObject
{
int c = 5;

static void Main()
{
Test t = new Test();
t.c.ToString();
t.f(ref t.c);
}

void f(ref int i)
{
}
}
[/CS]

The problem is that, when you call Int32.ToString(), the first parameter (the hidden this parameter) is passed in the same way as a "ref int" parameter would be passed. (Compare this to the call to f(ref int).) The generated MSIL is the same for both Int32.ToString() parameter 0 and Test.f() parameter 1: they use ldflda.

What Im saying is that, so far as the compilers concerned, when you do the "t.c.ToString()", the first parameter is essentially a "ref int" parameter. Since your class is derived from MarshalByRefObject (Form is ultimately derived from this class), passing this int member by-ref is illegal. If you were to comment out the derivation from MarshalByRefObject in the example above, the code would compile just fine.
 
Back
Top