Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

.net - members of class being overwritten when new instance created c++/cli

I have tried to implement the following:

1) Create a class that contains managed arrays 2) Create three instances of this class 3) Fill those arrays from three different data files. 4) Perform an analysis of this data

I have been able to compile code that performs task numbers one and two, however when I fill the arrays of the subsequent instances they seem to overwrite the data of the previous ones. I have tried to use a structure instead of a class. I have tried to use native arrays instead of managed arrays. I am obviously missing something here, but I can't find my answer anywhere that I have looked. Maybe I don't know the terminology to ask my question correctly.

Also, when I used the debugger I found that the garbage collector seemed to delete my data before I could perform my analysis on it.

Any help would be appreciated.

The problem is that the class instance LowE is overwritten with MedE when the button to load the second file is pushed.

I tried to ask this question without posting the entire project code and was not successful So, here is what I have:

File runData.h

#pragma once

#define MAXPOINTS 1536
#define LDBL_MAX 1.7976931348623158e+308
#define MAXORDER 10
#define MAXVOLUME 100.0


using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;


namespace EDCCalApp02 {


 public ref class  runData //:  public System::ComponentModel::Component
{
public:

       static array<double^> ^WPcoefs  = gcnew array<double^>(MAXORDER+1);

        static array<double ^,2> ^fData1  = gcnew array<double^,2>(MAXPOINTS,7);

       static array<int^,2> ^iData1  = gcnew array<int^,2>(MAXPOINTS,7);

        static array<int^,2> ^DataSize  = gcnew array<int^,2>(3,3);


      int numLines; 
      int numCols;
      int indx;
      int drops;


    runData(void)
    {


         for (int n = 0; n<1536; n++) {

             fData1[n,0] = 0.0;
             iData1[n,5] = 0;
             iData1[n,6] = 0; 
         }


    }


private:

    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

    void InitializeComponent(void)
    {
        components = gcnew System::ComponentModel::Container();
    }
#pragma endregion


};


}

File Form1.h

#pragma once

#define MAXPOINTS 1536
#define LDBL_MAX 1.7976931348623158e+308
#define MAXORDER 10
#define MAXVOLUME 100.0

#include "runData.h"
#include "Graph1.h"
#include "Global1.h"

namespace EDCCalApp02 {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;

array<double^> ^polyfit(array<double^,2> ^, int, int);
array<double^,2>  ^datasort(array<double^,2> ^, int);
double ldpow(  double , unsigned );
float poly_correlcoef( array<double^,2> ^ ,int, array<double^> ^, int  );
array<double^> ^get_xy_max_min( array<double^,2> ^ ,int );


array<double^,2> ^fillData(int,  array<double^> ^, runData ^ );
array<double^,2> ^makeCurve(array<double^,2> ^, array<double^,2> ^, array<double^,2> ^, double);
array<double^,2> ^makeCoefs(array<double^,2> ^, array<double^> ^);
runData  ^LoadData(array<String^> ^);

public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(void)
    {
        InitializeComponent();


        int numLines = 0, numCols=0;


  try{
  System::IO::StreamReader ^ sr = gcnew
  System::IO::StreamReader("WellPlate Volume Definitions.txt");
  array<String^> ^lines= System::IO::File::ReadAllLines("WellPlate Volume Definitions.txt");

  numLines = lines->Length;
  array<String^> ^Header=lines[0]->Split(',');
  numCols = Header->Length;

  staVolFile = gcnew array<String^,2>(numLines,numCols);

  for(int i=1;i<numLines;i++) {

     array<String^> ^lineSplit=lines[i]->Split(',');

     for(int j=0;j<numCols;j++){
       staVolFile[i-1,j] = lineSplit[j];
       }

     cbx_WellPlateList->Items->Add(staVolFile[i-1,0]);
    }

         sr->Close();
  }
            catch(IOException ^)       {
    MessageBox::Show(L"WellPlate Volume Definitions.txt

" 
        L"Was Not Read

"
        L"Please Check the File
"
        L"and Try Again");

        }

    }

protected:

    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::TextBox^  tbx_File1;
protected: 

protected: 
private: System::Windows::Forms::Label^  lbl_File1;
private: System::Windows::Forms::ComboBox^  cbx_WellPlateList;
private: System::Windows::Forms::TextBox^  tbx_File2;
private: System::Windows::Forms::TextBox^  tbx_File3;

private: System::Windows::Forms::Label^  lbl_Volume;
private: System::Windows::Forms::Button^  btn_Exit;

private:
public:

     array<String^,2>  ^staVolFile;
     static array<bool^> ^loaded = gcnew array<bool^>(4) {false,false,false,false};


    static  runData ^LowE = ( gcnew runData);
    static  runData ^MedE = ( gcnew runData);
    static  runData ^HighE = (gcnew runData);

private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;

    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

    void InitializeComponent(void)
    {
        System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
        this->tbx_File1 = (gcnew System::Windows::Forms::TextBox());
        this->lbl_File1 = (gcnew System::Windows::Forms::Label());
        this->cbx_WellPlateList = (gcnew     System::Windows::Forms::ComboBox());

        this->btn_Exit = (gcnew System::Windows::Forms::Button());
        this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
        this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
        this->SuspendLayout();
        // 
        // tbx_File1
        // 
        this->tbx_File1->Location = System::Drawing::Point(40, 100);
        this->tbx_File1->Name = L"tbx_File1";
        this->tbx_File1->Size = System::Drawing::Size(267, 20);
        this->tbx_File1->TabIndex = 0;
        this->tbx_File1->Text = L"Enter .csv File 1";

        // cbx_WellPlateList
        // 
        this->cbx_WellPlateList->FormattingEnabled = true;
        this->cbx_WellPlateList->Location = System::Drawing::Point(40, 40);
        this->cbx_WellPlateList->Name = L"cbx_WellPlateList";
        this->cbx_WellPlateList->Size = System::Drawing::Size(221, 21);
        this->cbx_WellPlateList->TabIndex = 2;
        this->cbx_WellPlateList->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::cbx_WellPlateList_SelectedIndexChanged);


        this->btnFile1->AutoSizeMode = System::Windows::Forms::AutoSizeMode::GrowAndShrink;
        this->btnFile1->Location = System::Drawing::Point(306, 98);
        this->btnFile1->Name = L"btnFile1";
        this->btnFile1->Size = System::Drawing::Size(25, 24);
        this->btnFile1->TabIndex = 7;
        this->btnFile1->UseVisualStyleBackColor = true;
        this->btnFile1->Click += gcnew System::EventHandler(this, &Form1::btnFile1_Click);

        // lbl_WellplateList
        // 
        this->lbl_WellplateList->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
            | System::Windows::Forms::AnchorStyles::Left) 
            | System::Windows::Forms::AnchorStyles::Right));
        this->lbl_WellplateList->AutoSize = true;
        this->lbl_WellplateList->Location = System::Drawing::Point(45, 25);
        this->lbl_WellplateList->Name = L"lbl_WellplateList";
        this->lbl_WellplateList->Size = System::Drawing::Size(98, 13);
        this->lbl_WellplateList->TabIndex = 28;
        this->lbl_WellplateList->Text = L"Wellplate Selection";
        // 
        // btn_Graph1
        // 
        this->btn_Graph1->BackColor = System::Drawing::Color::White;
        this->btn_Graph1->Location = System::Drawing::Point(348, 99);
        this->btn_Graph1->Name = L"btn_Graph1";
        this->btn_Graph1->Size = System::Drawing::Size(38, 23);
        this->btn_Graph1->TabIndex = 29;
        this->btn_Graph1->Text = L"G1";
        this->btn_Graph1->UseVisualStyleBackColor = true;
        this->btn_Graph1->Click += gcnew System::EventHandler(this, &Form1::btn_Graph1_Click);

        // 
        // btn_Create
        // 
        this->btn_Create->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
        this->btn_Create->Location = System::Drawing::Point(40, 292);
        this->btn_Create->Name = L"btn_Create";
        this->btn_Create->Size = System::Drawing::Size(75, 23);
        this->btn_Create->TabIndex = 33;
        this->btn_Create->Text = L"Create Cal";
        this->btn_Create->UseVisualStyleBackColor = true;
        this->btn_Create->Click += gcnew System::EventHandler(this, &Form1::btn_Create_Click);


        this->btn_Exit->Anchor = System::Windows::Forms::AnchorStyles::Bottom;
        this->btn_Exit->Location = System::Drawing::Point(657, 304);
        this->btn_Exit->Name = L"btn_Exit";
        this->btn_Exit->Size = System::Drawing::Size(75, 23);
        this->btn_Exit->TabIndex = 36;
        this->btn_Exit->Text = L"Exit";
        this->btn_Exit->UseVisualStyleBackColor = true;
        this->btn_Exit->Click += gcnew System::EventHandler(this, &Form1::btn_Exit_Click);

        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->BackColor = System::Drawing::Color::White;
        this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));
        this->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch;
        this->ClientSize = System::Drawing::Size(830, 348);
        this->Controls->Add(this->btn_Exit);
        this->Controls->Add(this->btn_Graph1);
        this->Controls->Add(this->lbl_WellplateList);

        this->Controls->Add(this->lbl_Order1);

        this->Controls->Add(this->lbl_Max1);
        this->Controls->Add(this->lbl_Min1);
        this->Controls->Add(this->tbx_Order1);


        this->Controls->Add(this->cbx_WellPlateList);
        this->Controls->Add(this->lbl_File1);
        this->Controls->Add(this->tbx_File1);
        this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
        this->Name = L"Form1";
        this->Text = L"EDC Calibration App";
        this->ResumeLayout(false);
        this->PerformLayout();

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, it turns out that the problem was with declaring the array static in the class. Apparently what I actually wanted was to declare the variable but create it in the initialization function. I had tried this earlier, but the compiler choked and I thought that there was a problem with doing it this way. But, once I was determined that this was the solution I was able to make it work. Static means that the memory is the same for all instances of the class which is a convenient way to share data common to all instances of a class. I realized that I could use this for another array in the same class that would no longer need to be read in each time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...