ref 或 out 参数必须是可赋值的变量 [英] A ref or out argument must be an assignable variable

查看:42
本文介绍了ref 或 out 参数必须是可赋值的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您不能像在那里那样在创建变量的同时将其作为引用传递.试试这个:

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
}

这篇关于ref 或 out 参数必须是可赋值的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆