خاصية على كل كائن من القائمة euqals ، ولكن يجب تعيينها مختلفة في حلقة

StackOverflow https://stackoverflow.com/questions/4625652

  •  30-09-2019
  •  | 
  •  

سؤال

لدي مشكلة في الكود الخاص بي. يجب أن يقوم الرمز بإنشاء قائمة بالكائنات من نوع الروبوت. يجب أن يتضمن كل روبوت منفذًا تسلسليًا واسمًا (ربما لاحقًا بعض الأطراف). لكن في هذه المرحلة ، لا أفهم ، لماذا يحصل أي روبوت على نفس الاسم - اسم آخر ميناء متاح في النظام.

لذا ، هل يمكن لأي شخص أن يخبرني ، لماذا؟ (في النهاية تقريبًا // tbd-mark) ، لا تتردد في التعليق أو تصحيح الباقي أيضًا. وأنا أعلم ، إنه كثير من الارتفاع وربما ليس من الناحية الجزئية أفضل رمز.

لذا شكرا لكم مقدما.

#define debug_enabled 0
#define exampleclass_enabled 0

#using <System.dll>

using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
using namespace System::Collections::Generic;
using namespace System::Timers;


public ref class Robot {
private: static String^ _name;
        static bool _active;

public:
    property String^ name
    {
        String^ get(){return _name;}
        void set (String^ newname) {_name = newname;} }
    static SerialPort^ port;
//  static String^ GetNameString() { return _name;}
    static bool IsActive() {return _active;}
//  static String^ SetName(String^ name) { _name->Copy(name);return _name;} 
    static bool SetActive(bool active) { _active = active; return _active;} 

};
public ref class CommunicatorClass
{
private:
       static System::Timers::Timer^ aTimer;
       static array<String^,2>^ commandList = gcnew array<String^,2>(6,2);


public: 
    static List<Robot^>^ _serialPortList = gcnew List<Robot^>();
    static int baudRate = 9600;
    static int dataBits = 8;
    static System::IO::Ports::StopBits stopBits = System::IO::Ports::StopBits::One;
    static System::IO::Ports::Parity parity = System::IO::Ports::Parity::None;
//  void Main(); /*initialisation of the Com-Ports*/
    static bool SendCommand(String^ command){
        //search in defined commandlist for command to send, if match, send int, if not set int to last default position(start) to wait for start as response
        int commandInArray;

        for(int i=0; i<commandList->Rank; i++)
            for (int j=0;j<1;j++) { //it doesnt care, if the command is the number or the word for it that equals the response
            if(command->Contains(commandList[i,0]))
                commandInArray = i;
            else
                commandInArray = commandList->Rank;
            }


        for each (Robot^ s in _serialPortList)
        {
            if (s->IsActive()){
                if (!s->port->IsOpen){
                    s->port->Open();
                } 

                try
                   {

                        s->port->DiscardInBuffer();
                        s->port->WriteLine(commandList[commandInArray,0]);
                        bool _temp = 0;


                        aTimer = gcnew System::Timers::Timer( 10000 );

                      // Set the Interval to 500 mseconds.
                      aTimer->Interval = 500;
                      aTimer->Enabled = true;

                        do  
                        {if (s->port->ReadLine()->Contains(commandList[commandInArray,1])) _temp = 1; // and in code of robot: /n after each line!
                        if (aTimer->Interval == 0) {
                            _temp = 1;
                            throw gcnew TimeoutException("Timeout on sending command to Robot - no response.");
                            }
                        }
                        while (_temp == 1);

                      }
            catch (TimeoutException ^) {
            return 0;}

                }
            }

        return 1;
    } /*sends the specified command to each robot marked active*/
    static bool refresh(){
        _serialPortList->Clear();
        CommunicatorClass::Main();
        return 1;
    }
    static void Main(){
        //initialize commands [*,0] and responses [*,1] in array
        commandList[0,0] = "8";
        commandList[0,1] = "Vor";
        commandList[1,0] = "6";
        commandList[1,1] = "Links";
        commandList[2,0] = "7";
        commandList[2,1] = "Zurueck";
        commandList[3,0] = "4";
        commandList[3,1] = "Rechts";
        commandList[4,0] = "5";
        commandList[4,1] = "Stop";
        commandList[5,0] = "";
        commandList[5,1] = "Start";


//          _serialPortList->Initialize;
        for each (String^ s in SerialPort::GetPortNames())
        {
#if debug_enabled
            Console::WriteLine("   {0}", s);
#endif

            Robot^ temp = gcnew Robot();
            temp->port = gcnew SerialPort(s,baudRate,parity,dataBits,stopBits);
            //temp->SetName(s);
            temp->name = s;
            _serialPortList->Add(temp); //TBD::error in dereferianciation ?!


        }
#if debug_enabled
        for each (Robot^ s in _serialPortList)
        {
            Console::WriteLine("   {0}", s->name);
        }
#endif
    }

};
هل كانت مفيدة؟

المحلول

public ref class Robot {
private: static String^ _name;
        static bool _active;

public:
    property String^ name
    {
        String^ get(){return _name;}
        void set (String^ newname) {_name = newname;} 
    }
    static SerialPort^ port;
//  static String^ GetNameString() { return _name;}
    static bool IsActive() {return _active;}
//  static String^ SetName(String^ name) { _name->Copy(name);return _name;} 
    static bool SetActive(bool active) { _active = active; return _active;} 
};

في تعريف الفصل الخاص بك للروبوت ، لديك كل شيء محدد على أنه ثابت. هذا يعني أنه سيتم مشاركته بين جميع حالات هذا الفصل ، ويمكن الوصول إليه كـ Robot::SerialPort, ، بدلاً من طلب مثيل للروبوت ، مثل Robot^ r; r->SerialPort.

قم بتبديل كل هؤلاء ليكونوا غير قاسيين ، وأعتقد أن هذا سيحل مشكلتك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top