A ref or out argument must be an assignable variable

与世无争的帅哥 提交于 2020-01-10 04:12:08

问题


I'm coding an application which can make a reverse proxy connection but I have a problem! The error is here: new Form1.ProxyConfig()

When I try to run it I get an error: "A ref or out argument must be an assignable variable"

private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (this.startToolStripMenuItem.Text == "Start")
    {
        var form2 = new Form2();

        if (form2.ShowDialog() != DialogResult.OK)
            return;

        int num1 = Form1.ProxyListenerStart(ref new Form1.ProxyConfig()
        {
            pclient_port = form2.ClientPort,
            pp_start = form2.LocalStartPort,
            pp_end = form2.LocalEndPort
        }, ref this._PN);

        if (num1 != 0)
            int num2 = (int) MessageBox.Show("Error " + num1.ToString());
        else startToolStripMenuItem.Text = "Stop";
    }
    else
    {
        Form1.ProxyListenerStop();

        startToolStripMenuItem.Text = "Start";
        listView1.Items.Clear();
        toolStripStatusLabel2.Text = "0";
    }
}
private struct ProxyConfig
{
    public int pclient_port;
    public int pp_start;
    public int pp_end;
}

回答1:


You cannot create a variable and pass it as a reference at the same time like you're doing there. Try this:

var config = new Form1.ProxyConfig()
{
    pclient_port = form2.ClientPort,
    pp_start = form2.LocalStartPort,
    pp_end = form2.LocalEndPort
};

int num1 = Form1.ProxyListenerStart( ref config, ref this._PN );

The reason is that it really wouldn't make any sense, consider the following scenario:

if( int.TryParse( "123", out new int() ) )
{
    // there's no way for us to actually use the value TryParse stored
    // into the out parameter, since it doesn't have a name
}


来源:https://stackoverflow.com/questions/24828842/a-ref-or-out-argument-must-be-an-assignable-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!