ref或out参数必须是可赋值变量

时间:2014-07-18 15:35:34

标签: c# proxy reverse

我正在编写一个可以进行反向代理连接的应用程序,但我遇到了问题! 错误在于:new Form1.ProxyConfig()

当我尝试运行它时,我收到一个错误:“ ref或out参数必须是可赋值变量

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 个答案:

答案 0 :(得分:5)

您无法创建变量并将其作为参考传递,就像您在那里一样。试试这个:

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

原因是它确实没有任何意义,请考虑以下情况:

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
}