在visual studio c ++中定义一般字符串

时间:2015-01-08 17:13:01

标签: string visual-studio-2010 c++-cli

我在C ++ VS2010中编写了一个项目。它有一些GUI,然后我想将一些变量(字符串)从一个函数传递给另一个。确切地说,我已经将两个复选框定义为

    this->M1_GR1->AutoSize = true;
    this->M1_GR1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0)));
    this->M1_GR1->Location = System::Drawing::Point(7, 27);
    this->M1_GR1->Margin = System::Windows::Forms::Padding(2, 4, 2, 4);
    this->M1_GR1->Name = L"M1_GR1";
    this->M1_GR1->Size = System::Drawing::Size(73, 19);
    this->M1_GR1->TabIndex = 95;
    this->M1_GR1->Text = L"M1-GR1";
    this->M1_GR1->UseVisualStyleBackColor = true;
    this->M1_GR1->CheckedChanged += gcnew System::EventHandler(this,&config::M1_GR1_CheckedChanged);

    this->M1_GR2->AutoSize = true;
    this->M1_GR2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9,System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,             static_cast<System::Byte>(0)));
    this->M1_GR2->Location = System::Drawing::Point(7, 63);
    this->M1_GR2->Margin = System::Windows::Forms::Padding(2, 4, 2, 4);
    this->M1_GR2->Name = L"M1_GR2";
    this->M1_GR2->Size = System::Drawing::Size(73, 19);
    this->M1_GR2->TabIndex = 96;
    this->M1_GR2->Text = L"M1-GR2";
    this->M1_GR2->UseVisualStyleBackColor = true;
    this->M1_GR2->CheckedChanged += gcnew System::EventHandler(this,&config::M1_GR2_CheckedChanged);

然后这些复选框有一个条件。如果用户选择其中一个,则另一个将显示为灰色,然后将保存字符串M1_GR1->TextM1_GR1->Text之一。

private: System::Void M1_GR1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
            String^ M1_GR;
            if (M1_GR1->Checked == true) {
                M1_GR2->Enabled = false;
                M1_GR = M1_GR1->Text;
            }
            if (M1_GR1->Checked == false) {
                M1_GR2->Enabled = true;
            }
        }

private: System::Void M1_GR2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
            String ^ M1_GR;
            if (M1_GR2->Checked == true) {
                M1_GR1->Enabled = false;
                M1_GR = "M1-GR2";
            }
            if (M1_GR2->Checked == false) {
                M1_GR1->Enabled = true;
            }
        }

现在,如果用户选中了一个后面的框,我想在*.csv文件中保存一个字符串,比如M1-GR1

private: System::Void config_Load(System::Object^  sender, System::EventArgs^  e) {
             load_csv();
         }
         void load_csv()
         {
             StreamReader^ sr = File::OpenText("config.csv");
             String^ buff = "";
             array<Char>^chars = {','};
             array<String^>^ arr;

             while (buff = sr->ReadLine()) {
                 arr = buff->Split(chars);
                 if (buff!=""){
                     M1_GR = arr[0];
                 }
                 else
                 {

                 }
             }
             sr->Close();
         }

当我想调试代码时,我遇到以下错误:

config.h(1800): error C2065: 'M1_GR' : undeclared identifier

请您告诉我如何将字符串M1_GR传递给M1_GR

非常感谢你提前发表意见!

聚苯乙烯。示例版本将在以下链接中上载。通过:测试

http://www.mediafire.com/download/3j5302m17ngvrvn/test.zip

1 个答案:

答案 0 :(得分:0)

从它的外观来看,似乎你得到了这个错误,因为你试图在不声明的情况下使用M1_GR(如错误信息所述)。 如果我正确理解你的问题,并且你试图保存M1_GR字符串,保存在你检查的一个复选框上,在.csv文件中,那么我会改变一些事情:

  1. 将M1_GR(通过引用)作为参数传递给M1_GR1_CheckedChanged()和M1_GR2_CheckedChanged(),如下所示
  2. private: System::Void M1_GR1_CheckedChanged(System::Object^ sender, System::EventArgs^ e, String% M1_GR) {
    if (M1_GR1->Checked == true) { M1_GR2->Enabled = false; M1_GR = M1_GR1->Text; } if (M1_GR1->Checked == false) { M1_GR2->Enabled = true; } }

    private: System::Void M1_GR2_CheckedChanged(System::Object^ sender, System::EventArgs^ e, String% M1_GR) { if (M1_GR2->Checked == true) { M1_GR1->Enabled = false; M1_GR = "M1-GR2"; } if (M1_GR2->Checked == false) { M1_GR1->Enabled = true; } }

    你最初在两个函数中声明它的方式使它只是函数的本地化,因此在你的函数失去它们的范围之后它并不存在。

    1. 至于没有。在上面的图1中,将一个字符串引用传递给你的config_Load()函数并将相同的参数传递给你的load_csv()函数(在这种情况下,我会按值传递它,因为看起来你对它是否被修改不感兴趣)。

    2. 最后,我会稍微改变load_csv()的实现方式:

    3. void load_csv(String M1_GR) { //StreamReader^ sr = File::OpenText("config.csv"); //--Use File::OpenWrite() instead of File::OpenText() //--OpenText() opens the file just for reading yet we want to write to it //--Explore just Open() for more flexibility

               FileStream^ fs = File::OpenWrite("config.csv");
      
               //-- I didn't understand what the code below is trying to do so I commented it out
               /*String^ buff = "";
               array<Char>^chars = {','};
               array<String^>^ arr;
      
               while (buff = sr->ReadLine()) {
                   arr = buff->Split(chars);
                   if (buff!=""){
                       M1_GR = arr[0];
                   }
                   else
                   {
      
                   }
               }
               sr->Close();*/
      
               try {
                 //Better memory allocation can be achieved by allocating just enough bytes one need
                 array<Byte>^ M1_GR_bytes = (gcnew UTF8Encoding(true)->GetBytes(M1_GR);
      
                 fs->Write(M1_GR_bytes , 0, M1_GR_bytes->Length); //--Write M1_GR to your file
               }
               finally {
                 if (fs) delete (IDisposable^) fs;
               }
           }
      

      FileStream^ fs = File::OpenWrite("config.csv"); //-- I didn't understand what the code below is trying to do so I commented it out /*String^ buff = ""; array<Char>^chars = {','}; array<String^>^ arr; while (buff = sr->ReadLine()) { arr = buff->Split(chars); if (buff!=""){ M1_GR = arr[0]; } else { } } sr->Close();*/ try { //Better memory allocation can be achieved by allocating just enough bytes one need array<Byte>^ M1_GR_bytes = (gcnew UTF8Encoding(true)->GetBytes(M1_GR); fs->Write(M1_GR_bytes , 0, M1_GR_bytes->Length); //--Write M1_GR to your file } finally { if (fs) delete (IDisposable^) fs; } }