不能使用公共静态类,或者不知道如何使用

时间:2014-06-05 07:19:43

标签: c# static

我有一个公共静态类Cashier

namespace MPP_TCP_Client
{
    public static class Cashier
    {
        public static IPAddress localAddr = IPAddress.Parse("127.0.0.1");

        public static TcpClient Connect()
        {
            try
            {
                return new TcpClient(Util.localAddrStr, Util.port);
            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("ArgumentNullException: {0}", ane);
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException: {0}", se);
            }
            catch (NullReferenceException nre)
            {
                Console.WriteLine("Null Reference Exception: {0}", nre);
            }
            return null;
        }

        // etc.
    }
}

现在我想使用Connect方法:

namespace Mpp_TCP_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press enter to start client");
            Console.ReadKey();
            TcpClient whatever = Cashier.Connect(); // Cashier is not being seen by Visual Studio
        }
    }
}

1 个答案:

答案 0 :(得分:1)

正如上面提到的Dennis_E(我在发布此答案后注意到了他的评论):

您的命名空间有不同的外壳。

Cashier班级使用:MPP_TCP_ClientProgram班级使用:Mpp_TCP_Client

尝试匹配命名空间:

namespace MPP_TCP_Client // This should now be the same as the Cashier class
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press enter to start client");
            Console.ReadKey();
            TcpClient whatever = Cashier.Connect(); // Cashier is not being seen by Visual     Studio
        }
    }
}

如果这是故意的,请添加以下using语句:

using MPP_TCP_Client;