2013-01-06 2 views
0

다른 클래스의 한 클래스 멤버에 액세스하는 데 문제가 있습니다. 두 클래스를 선언했습니다. 첫 번째는 Form1이고 두 번째는 packet_class입니다. 클래스의 더 짧은 버전이 아래에 있습니다. 내 문제는 packet_send_data 클래스에서 serialPort1을 업데이트하려고 시도하고 있습니다. serialPort1 개체에 액세스하여 packet_class :: packet_send_data를 통해 데이터를 보낼 수 있습니까? 나는 packet_class 인스턴스화 된 packet_class 객체에 success serialPort1을 전달하려고 시도했다.
귀하의 도움에 감사드립니다 (간단한 코드 예제가 바람직 함). Form1packet_class 인스턴스화 한 단지 생성자 파라미터로 전달하는 경우
감사 Managed C++/CLI 다른 클래스의 serialport1에 액세스

namespace Form_Example { 
using namespace System; 
using namespace System::ComponentModel; 
using namespace System::Collections; 
using namespace System::Windows::Forms; 
using namespace System::Data; 
using namespace System::Drawing; 

public ref class Form1 : public System::Windows::Forms::Form 
{ 
    //Form Initialisation ETC 
public: 
    Form1(void) 
    { 
     this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components)); 
    } 
protected: 
public: System::IO::Ports::SerialPort^ serialPort1; 
}; 

}는

packet_class는

ref class packet_class 
{ 
public: 
    //Constructor Prototype 
packet_class(void); 
void packet_send_data(void); 
String^ data_to_send; // Create an array to store 100 integers 
}; 

//Packet_class Prototype 
packet_class::packet_class(void) 
{ 
data_to_send="SEND ABC"; 
} 

void packet_class::packet_send_data(void) 
{ 
    //I want to access serialPort in the packet_class function here 
    //serialPort1->Write(data_to_send); 
} 

답변

1

이하이다.

ref class packet_class 
{ 
    SerialPort^ serial; 

    packet_class(SerialPort^ serial) 
    { 
     this->serial = serial; 
     data_to_send="SEND ABC"; 
    } 
}; 

// Somewhere in Form1.... 
packet_class packet = gcnew packet_class(this->serialPort1); 
+0

Perfect! 감사 –

관련 문제