2014-10-21 5 views
0

일부 사용자는 C#의 다른 클래스에서 인스턴스화하고 개체를 처리하는 방법을 알려줄 수 있습니까?한 클래스의 개체를 다른 인스턴스로 인스턴스화

처음 세 문자열 변수 (secureID, EmailAddress 및 PhoneNum)를 다른 클래스에 배치하고이 클래스의 값을 할당하려고합니다. C++에서는 친구 클래스를 사용하고 C#과 같은 것을 찾을 수 없습니다.

또한 설명 :이 코드를 갖고 싶습니다

:

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

과 자신의 클래스에 넣습니다. 공용 클래스 myMsg를 호출 할 수 있습니다. 아래 클래스 프로그램에서 myMsg을 설치하고 그 필드에 값을 myMsg.SecureId = strDataParse [0]처럼 할당 할 수 있습니다. 클래스 프로그램을 통해 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(); 



     } 
    } 



} 
+0

생성자에 전달합니다. 나는 단 하나의 클래스를 본다. 귀하의 질문과 관련이있는 코드로 코드를 좁힐 수 있습니까? – BradleyDotNET

+0

언급 한 변수는 정적입니다. 그것들은 클래스 변수이므로 Program 클래스의 인스턴스를 생성 할 필요가 없습니다. Program.SecureId와 같은 것을 사용하여 액세스 할 수 있어야하지만 getter 및 setter를 정의해야합니다. –

+0

나는 이것에 대해 좀 더 명확히하려고 노력했다. 도움이되는지 알려주세요. –

답변

0

클래스와 멤버는 기본적으로 비공개입니다. 학급 밖에서 눈에 보이게하려면 클래스와 멤버를 공개 또는 내부로 표시해야합니다.

중첩 된 형식 (다른 클래스 내에 정의 된 클래스)이 있지만 C#에는 친구 클래스가 없다는 것이 맞습니다.

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

가 공용 또는 내부했고,이 분야에게 필요하지 않지만 속성을하는 것이 일반적입니다 경우 Program.SecureId로 액세스 할 것이라고 위의 의견은 정확합니다.

+0

나는 클래스를 public으로 가지고 있었지만 여전히 프로그램 클래스의 필드에 접근 할 수는 없었다. 내가 분명히하기 위해 위의 게시물을 업데이 트되었습니다. 이 인스턴스에서 get/set 메서드가 도움이 될까요? –

+0

ok, msg 클래스를 public (public class msg)로 만들고 클래스의 필드를 public (public static string SecureId)로 만듭니다. 정적으로 만들 이유는 없지만 classname.fieldname (msg . 안전). 비 정적 클래스 (msg myMsg = new msg())의 인스턴스를 인스턴스화하려면 클래스 변수 (myMsg.SecureId)로 액세스하십시오. 당신은 그것들을 속성 (get/set)으로 만들 수 있지만이 경우에는 중요하지 않습니다. – jy247

관련 문제