【问题标题】:How can I call a function from a class如何从类中调用函数
【发布时间】:2015-10-22 00:10:53
【问题描述】:

我真的很困惑。 我的 Program.cs 如下:

using System;
using System.IO;
using System.Net.NetworkInformation;
using System.Collections.Generic;

namespace PingApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = @"PingLog.csv";
            {
                using (var writer = new StreamWriter(filename, true))
                {

                }
            }
        }
    }
}

然后我有一个名为 WebSitePing.cs 的类,这是我拥有所有代码的地方。这是我的 WebSitePing.cs 类:

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;

namespace PingApp
{
    public class WebSitePing
    {
        public void Ping()
        {
            var lstWebSites = new List<string>
            {
                "www.mearstransportation.com",
                "www.amazon.com",
                "www.ebay.com",
                "www.att.com",
                "www.verizon.com",
                "www.sprint.com",
                "www.centurylink.com",
                "www.yahoo.com"
            };
            foreach (string website in lstWebSites)
            {
                var roundTripTime = new List<string>();
                for (var i = 0; i < 4; i++)
                {
                    try
                    {
                        var myPing = new Ping();
                        var reply = myPing.Send(website, 1000);
                        if (reply != null)
                        {
                            Console.WriteLine("{0}, {1}", website, string.Join(" , ", roundTripTime));
                        }
                    }
                    catch
                    {
                        Console.WriteLine("ERROR: You have some TIMEOUT issue");
                    }
                }
            }
        }
    }
}

我必须将我的 WebSitePing.cs 类调用到 Program.cs 中,以便我可以创建 PingLog.csv 文件并运行 WebSitePing.cs 中的代码,我的 ping 的所有结果都将保存到该文件中我从 Program.cs 创建。 有人可以帮我弄清楚我该怎么做吗?

【问题讨论】:

  • 在您的Program.cs 文件中,您需要实例化您的WebSitePing 类。然后,您将能够使用该实例来调用您的 Ping() 方法。
  • 或者将您想要调用的方法设为静态(它似乎不依赖任何实例化变量),但我同意更好的方法可能只是实例化 WebSitePing
  • 通过在Program.cs 中执行var a = new WebSitePing() 创建WebSitePing 对象的实例。然后只需通过 a.Ping() 调用该方法。
  • 我是c#新手,能给我举个例子吗?

标签: c# function class ping


【解决方案1】:

你的问题在上面的cmets中基本得到了回答,但是这里是:

namespace PingApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var pinger = new WebSitePing();

            pinger.Ping();
        }
    }
}

除此之外,您还需要将 using 块移动到 Ping() 方法中,而不是在 Program.cs 本身内。这样,您将不使用Console.WriteLine(),而是将输出附加到您的StreamWriter

或者,您可以设置Console 的输出,就像@Enigmativity 的答案一样,这会将您的Console.WriteLine() 调用重定向到StreamWriter 而不是调试器控制台。

【讨论】:

  • 但是在@Enigmativity 的回答中,它返回的是网站而不是 ping 结果,我认为我不会浏览 WebSitePing.cs 中的所有代码
【解决方案2】:

试试这个:

string filename = @"PingLog.csv";
{
    using (var writer = new StreamWriter(filename, true))
    {
        Console.SetOut(writer);
        var ping = new WebSitePing();
        ping.Ping();
    }
}

您不仅必须创建WebSitePing 的实例才能调用.Ping(),还必须将Console 的输出设置为writer

最好让Ping() 返回IEnumerable&lt;string&gt;,然后写出这些行。这将是一个更清洁的程序。


WebSitePing 的代码有点损坏。这是最简单的解决方法。

public class WebSitePing
{
    public void Ping()
    {
        var lstWebSites = new List<string>
            {
                "www.mearstransportation.com",
                "www.amazon.com",
                "www.ebay.com",
                "www.att.com",
                "www.verizon.com",
                "www.sprint.com",
                "www.centurylink.com",
                "www.yahoo.com"
            };
        foreach (string website in lstWebSites)
        {
            var roundTripTime = new List<long>();
            for (var i = 0; i < 4; i++)
            {
                using (var myPing = new Ping())
                {
                    var reply = myPing.Send(website, 1000);
                    if (reply != null)
                    {
                        roundTripTime.Add(reply.RoundtripTime);
                    }
                }
            }
            Console.WriteLine("{0}, {1}", website, String.Join(", ", roundTripTime));
        }
    }
}

【讨论】:

  • 如果我尝试这个,我得到了 .csv 文件中的网站,但没有 ping 结果,如何打印 ping 结果?
  • @NewDev - 您在WebSitePing 中的代码不正确。从.Send() 电话的回复中没有得到时间。
  • 你知道我该如何解决吗?为什么不正确?当我将所有内容都放在一个文件中时,它就可以工作了。
  • @NewDev - 我已经添加了修复程序。
  • 太完美了,非常感谢....我整天都在尝试这样做-我正在尝试学习C#,有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多