【问题标题】:Instantiating an object of one class in another在另一个类中实例化一个类的对象
【发布时间】:2014-10-21 00:53:19
【问题描述】:

谁能告诉我如何在 C# 中的另一个类中实例化和对象。

这里的前 3 个字符串变量(secureID、EmailAddress 和 PhoneNum)我想将它们放在另一个类中,并在这个类中为它们赋值。在 c++ 中,我们会使用朋友类,在 C# 中找不到类似的东西。

进一步说明:

我想用这个代码:

static string SecureId;
  static string EmailAddress;
  static string PhoneNum;

并将它放在它自己的类中。让我们称之为公共类 myMsg。我想在下面的类 Program 中实例化 myMsg,并能够为其字段分配值,例如 myMsg.SecureId = strDataParse[0]。我在通过类 Program 访问 myMsg 字段时遇到问题。希望对您有所帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace EmailSMSSocketApp
{

   class Program
    {

        static string SecureId;
        static string EmailAddress;
        static string PhoneNum;

        static byte[] Buffer { get; set; }
        static Socket _socket;
        static void Main(string[] args)
        {
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.Bind(new IPEndPoint(IPAddress.Any, 1234));
            _socket.Listen(100);

            Socket accepted = _socket.Accept();
            Buffer = new byte[accepted.SendBufferSize];
            int bytesRead = accepted.Receive(Buffer);
            byte[] formatted = new byte[bytesRead];

            for (int i = 0; i < bytesRead; i++)
            {
                formatted[i] = Buffer[i];

            }
            string strData = Encoding.ASCII.GetString(formatted);
            //Console.Write(strData + " ASCII to strData" + "\r\n");
            string[] strDataParse = strData.Split(',');
            foreach (string word in strDataParse)
            {
               // Console.WriteLine(word);
                SecureId = strDataParse[0];
                EmailAddress = strDataParse[1];
                PhoneNum = strDataParse[2];

            };

            Console.WriteLine(SecureId + " Outside loop");
            Console.WriteLine(EmailAddress + " Outside loop");
            Console.WriteLine(PhoneNum + " Outside loop");


            Console.Read();
            _socket.Close();
            accepted.Close();



        }
    }



}

【问题讨论】:

  • 将它们传递给构造函数。我只看到一门课;您能否将代码缩小到与您的问题相关的代码?
  • 你提到的变量是静态的。它们是类变量,因此您无需创建 Program 类的实例。您应该能够使用类似 Program.SecureId 的方式访问它们,但您需要定义 getter 和 setter。
  • 我试图进一步澄清这一点。让我知道这是否有帮助。

标签: c# .net class methods


【解决方案1】:

类和成员默认是私有的。您需要将类和成员标记为 public 或 internal 才能在类外可见。

你说得对,尽管有嵌套类型(在其他类中定义的类),但 C# 中没有友元类。

Why does C# not provide the C++ style 'friend' keyword?

如果它是公共的或内部的,那么您将作为 Program.SecureId 访问它的上面的评论是正确的,并且通常的做法是使该字段成为属性,尽管不是必需的。

【讨论】:

  • 我将课程设置为公共课程,但仍然不允许我访问程序课程中的字段。我更新了上面的帖子以澄清。 get/set 方法在这种情况下会有帮助吗?
  • 好的,将 msg 类设为 public(public class msg),将类中的字段设为 public(public static string SecureId),我认为没有理由将它们设为静态,但如果你想访问它们作为 classname.fieldname (msg.SecureId)。要实例化非静态类的实例 (msg myMsg = new msg()),请将它们作为类变量 (myMsg.SecureId) 访问。您可以将它们设为属性(获取/设置),但在这种情况下并不重要
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 2018-01-12
  • 1970-01-01
相关资源
最近更新 更多