公共静态双名称()的返回字符串
本文关键字:返回 字符串 静态 | 更新日期: 2023-09-27 18:27:59
我有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"
}
im使用windows窗体应用程序
不能从类型为double
的函数返回string
。
问题的解决方案
检查零返回值有什么问题?
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";
}
有很多其他方法可以做到这一点
例如,您可以使用
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"; } }
或者,您可以在
PingCheck
中抛出异常,然后在buttonX1_Click
中捕获它。或者,您实际上可以使您的方法返回
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而言,这似乎是不正确的。您的方法返回格式化的数字或错误消息。这很明显,也很不方便。
好吧,让我们做对吧。首先,任何方法都应该做自己的工作,在你的情况下,工作是计算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();
将方法更改为:
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();
}