-1

您好,我正在尝试在 C# 中创建一个按钮,如果您按下它。它应该从附加的 txt 文件中生成一个关于 IP 地址是什么的消息框。但是我遇到了无法修复的错误。我想我的返回类型混淆了,我一直觉得这里是代码。

private String getIPAddress()
    {

        String x;

        using (TextReader configfile = File.OpenText("PC104Configs.txt"))
            while (configfile.Peek() > -1)  // If therre are no more characters in this line
            {
                x = configfile.ReadLine();

                if (x.Length == 0)
                {
                    // This is a blank line
                    continue;
                }

                if (x.Substring(0, 1) == ";")
                {
                    // This is a comment line
                    continue;
                }

                if (x == trueIP)
                {
                    // This is the real deal
                    testPort = configfile.ReadLine();
                    testIP = trueIP;
                    return MessageBox.Show(trueIP);
                }
            }  // End of 'while' there are more characters loop

        UnitToTest.Text = "";

        MessageBox.Show("Specified Configuration Not Found!");

        return (false);
    }


    private void btnSendConfig_Click(object sender, EventArgs e)
    {
        getIPAddress();
    }
4

3 回答 3

2

一方面,您的“getIPAddress”函数应该返回一个字符串。但是,您让它返回一个布尔值 (false)。我认为你真的需要返回'X'。另外,我怀疑您是否真的想从 MessageBox.Show(trueIP) 返回结果。

于 2012-07-26T01:18:08.717 回答
2

您正在从期望返回字符串的函数返回布尔值。因此,这:

private String

..不允许返回,这个:

return (false);

..或者

return MessageBox.Show(trueIP);

..false 是布尔值,并且 Show() 方法返回一个 DialogResult,您的函数必须返回一个字符串。

于 2012-07-26T01:19:01.120 回答
0

您声明getIPAddress返回 a String,但随后您尝试返回 aDialogResult和 a bool

听起来您需要更准确地定义getIPAddress应该做什么。函数签名意味着您将返回一个包含一个或多个 IP 地址的字符串(或一组字符串),但您的代码似乎想要做的是弹出一个带有找到的第一个 IP 地址的消息框。

于 2012-07-26T01:18:45.383 回答