从public static double name()返回字符串

时间:2015-12-23 08:22:06

标签: c#

我有richTextBox1从pingcheck(string,int)获得结果,它工作正常 但有一件事我想要添加它当toaltime = 0它在richTextBox1中显示“连接不可用” 这是我的代码

public static double pingcheck(string host, int echonum) // i tried to change to (string host ,int echonum,string result) and add this string if total time =0 but its not retrieve it i tried to create public string but couldn't take string
    {
        long totaltime = 0;
        int timeout = 120;
     Ping pingsender = new Ping();//send ping

        for (int i = 0; i < echonum; i++)
        {
            PingReply replay = pingsender.Send(host, timeout);
            if (replay.Status == IPStatus.Success)
            {
            totaltime += replay.RoundtripTime;
            }
            else if(totaltime==0)
            {
         string getipstatue = "the connectin is not available ";//i want show this text in richTextBox if total time is=0
            }


        }
        return totaltime / echonum;        
    }
private void buttonX1_Click(object sender, EventArgs e)
    {
richTextBox1.Text += (pingcheck("8.8.8.",1))+Environment.NewLine; //this will show replay  
richTextBox2.Text += (pingcheck("8.8.1.",1))+Environment.NewLine;//i want this one get"the connection is not available pleas check" 
}

即时通讯使用Windows窗体应用程序

3 个答案:

答案 0 :(得分:4)

您无法从string类型的函数返回double

问题的解决方案

检查归零值有什么问题?

public static double PingCheck(string host, int echonum) 
{
    long totaltime = 0;
    int timeout = 120;
    Ping pingsender = new Ping();

    for (int i = 0; i < echonum; i++)
    {
        PingReply replay = pingsender.Send(host, timeout);
        if (replay.Status == IPStatus.Success)
        {
            totaltime += replay.RoundtripTime;
        } else {
            return 0.0;
        }
    }

    // at least, one of them should be double to avoid integer division
    return (double)totaltime / echonum; 
}

private void buttonX1_Click(object sender, EventArgs e)
{
    var pingCheckResult = PingCheck("8.8.8.", 1);

    richTextBox1.Text += pingCheckResult > 0 
        ? pingCheckResult.ToString("F2") + Environment.NewLine 
        : "the connectin is not available";
}

还有很多其他方法可以做到这一点

  1. 例如,您可以使用out参数并使用以下定义创建方法:

    public static bool PingCheck(string host, int echonum, out double approximateTime) 
    {
        long totaltime = 0;
        int timeout = 120;
        Ping pingsender = new Ping();
    
        for (int i = 0; i < echonum; i++)
        {
            PingReply replay = pingsender.Send(host, timeout);
            if (replay.Status == IPStatus.Success)
            {
                totaltime += replay.RoundtripTime;
            } 
            else 
            {
                approximateTime = 0.0;
                return false;
            }
        }
    
        // at least, one of them should be double to avoid integer division
        approximateTime = (double)totaltime / echonum;
        return true;
    }
    
    private void buttonX1_Click(object sender, EventArgs e)
    {
        double appTime;
        if (PingCheck("8.8.8.", 1, out appTime))
        {
            richTextBox1.Text += appTime.ToString("F2") + Environment.NewLine;
        } else {
            richTextBox1.Text += "the connectin is not available";
        }
    }
    
  2. 或者您可以在PingCheck中抛出异常并在buttonX1_Click中抓住它。

  3. 或者您实际上可以让您的方法返回string并执行以下操作:

    public static string PingCheck(string host, int echonum) 
    {
        long totaltime = 0;
        int timeout = 120;
        Ping pingsender = new Ping();
    
        for (int i = 0; i < echonum; i++)
        {
            PingReply replay = pingsender.Send(host, timeout);
            if (replay.Status == IPStatus.Success)
            {
                totaltime += replay.RoundtripTime;
            } 
            else 
            {
                return "the connectin is not available";
            }
        }
    
        return (totaltime / echonum).ToString("F2");        
    }
    

    然而,对我而言,就OOP来说似乎是不正确的。您的方法返回格式化的数字或错误消息。这非常明显且不方便。

答案 1 :(得分:1)

好的,让我们做对。首先,任何方法都应该做自己的工作,在你的情况下,工作就是计算 ping主机的平均时间pingcheck是什么意思?让我们更改名称:

  // 120 was a magic number, now it's a parameter
  // do you really want to pass attempts each time you call the method? No
  public static Double PingAverageTime(String host, int attempts = 3, int timeout = 120) {
    // Validate the input
    if (String.IsNullOrEmpty(host))
      throw new ArgumentNullError("host");
    else if (attempts <= 0)
      throw new ArgumentOutOfRangeError("attempts");
    else if (timeout < 0)
      throw new ArgumentOutOfRangeError("timeout");

    Double totalTime = 0; // Double: we don't want integer divison problems

    using (Ping ping = new Ping()) { // IDisposable - into using!
      PingReply replay = ping.Send(host, timeout);

      if (replay.Status == IPStatus.Success)
        totalTime += replay.RoundtripTime;
      else // not succeeded: so the time is infinite
        return Double.PositiveInfinity; // no connection - average time is infinite
    }

    return totalTime / attempts;
  }

然后使用方法:

  Double time = PingAverageTime("8.8.8.", 1);

  richTextBox1.Text += Double.IsInfinity(time) 
    ? "the connectin is not available " 
    : time.ToString();

答案 2 :(得分:0)

将您的方法更改为:

public static string pingcheck(string host, int echonum) 
{
    ...

        else if(totaltime==0)
        {
            return "the connectin is not available ";//i want show this text in richTextBox if total time is=0
        }

    ...
    return (totaltime / echonum).ToString(); 
}