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

本文关键字:赋值 变量 out 参数 ref | 更新日期: 2023-09-27 18:13:26

我正在编写一个应用程序,可以使反向代理连接,但我有一个问题!错误在这里: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;
}

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

你不能像刚才那样创建一个变量并同时把它作为引用传递。试试这个:

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
}