Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi,

 

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

 

//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

Edited by divil
Posted

Ah, yes. This is a good one. A more compact example of what you're 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 I'm saying is that, so far as the compiler's 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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...