System :: Collections :: Generic :: Custom类对象列表c ++ / cli

时间:2018-01-23 05:12:38

标签: c++-cli

我正在尝试创建一个自定义类列表" Bandset"到Singleton类中的System :: Collections :: Generic :: List。但每当我向列表添加任何新项目时,所有以前的元素也会变得相同。请找到以下代码。

BandsetData.h

namespace ConfigToolApplicationData {
using namespace System;
using namespace System::Data;

public ref class Bandset
{
private:
    String ^bandsetTabName;
    DataTable ^bandsetTable;

public:     
    Bandset()
    {
        bandsetTable = gcnew DataTable();
    }       
    static property String^ Name { String^ get() { return bandsetTabName; } void set(String ^ value) { bandsetTabName = value; }};
    static property DataTable^ Table { DataTable^ get()
    {
        if (!bandsetTable)
            bandsetTable = gcnew DataTable();
        return bandsetTable;
    }
    void set(DataTable ^ value)
    {
        bandsetTable = value;
    }
    };
};
};

SharedData.h

namespace ConfigToolApplicationData {
using namespace System;
using namespace System::Data;

public ref class SharedData
{
private:
    System::Collections::Generic::List<String^> ^tabList;
    static System::Collections::Generic::List<Bandset^> ^bandsetTabList;
    SharedData() {}
    SharedData(const SharedData%) { throw gcnew System::InvalidOperationException("singleton cannot be copy-constructed"); }
    static SharedData m_instance;
    static int awardSymbolTableCount;
    static int bandsetTableCount;
    static int controlTableCount;
    static int paylineTableCount;
    static int awardSymbolTableIndex;
    static int bandsetTableIndex;
    static int controlTableIndex;
    static int paylineTableIndex;

public:
    static property SharedData^ Instance { SharedData^ get() { return %m_instance; } }
    static property int AwardSymbolTableCount { int get() { return awardSymbolTableCount; } void set(int value) { awardSymbolTableCount = value; }};
    static property int BandsetTableCount { int get() { return bandsetTableCount; } void set(int value) { bandsetTableCount = value; }};
    static property int ControlTableCount { int get() { return controlTableCount; } void set(int value) { controlTableCount = value; }};
    static property int PaylineTableCount { int get() { return paylineTableCount; } void set(int value) { paylineTableCount = value; }};
    static property int AwardSymbolTableIndex { int get() { return awardSymbolTableIndex; } void set(int value) { awardSymbolTableIndex = value; }};
    static property int BandsetTableIndex { int get() { return bandsetTableIndex; } void set(int value) { bandsetTableIndex = value; }};
    static property int ControlTableIndex { int get() { return controlTableIndex; } void set(int value) { controlTableIndex = value; }};
    static property int PaylineTableIndex { int get() { return paylineTableIndex; } void set(int value) { paylineTableIndex = value; }};
    static property System::Collections::Generic::List<String^>^ tabNameList
    { System::Collections::Generic::List<String^>^ get()
    {
        if (!m_instance.tabList)
        {
            m_instance.tabList = gcnew System::Collections::Generic::List<String^>();
            m_instance.tabList->Clear();
        }
        return m_instance.tabList;
    }
    }
    static property System::Collections::Generic::List<Bandset^>^ bandsetTabNameList
    { System::Collections::Generic::List<Bandset^>^ get()
    {
        if (!m_instance.bandsetTabList)
        {
            m_instance.bandsetTabList = gcnew System::Collections::Generic::List<Bandset^>();
            m_instance.bandsetTabList->Clear();
        }
        return m_instance.bandsetTabList;
    }
    }

    void SharedData::RemoveBandsetTab(String ^name)
    {
        for (int index = 0; index < m_instance.bandsetTabList->Count; index++)
        {
            System::Diagnostics::Debug::WriteLine(m_instance.bandsetTabNameList[index]->Table->TableName);
            if (m_instance.bandsetTabList[index]->Name->Equals(name))
                m_instance.bandsetTabNameList->RemoveAt(index);
        }
        System::Diagnostics::Debug::WriteLine(m_instance.bandsetTabNameList->Count);
    }
    void SharedData::AddBandsetTab(String ^name)
    {
        Bandset^ bandset = gcnew Bandset();

        bandset->Name = name;
        bandset->Table->TableName = name;
        System::Diagnostics::Debug::WriteLine("Before");
        System::Diagnostics::Debug::WriteLine(m_instance.bandsetTabNameList->Count);
        for (int index = 0; index < m_instance.bandsetTabNameList->Count; index++)
        {
            System::Diagnostics::Debug::WriteLine(index);
            System::Diagnostics::Debug::WriteLine(m_instance.bandsetTabNameList[index]->Name);
            System::Diagnostics::Debug::WriteLine(m_instance.bandsetTabNameList[index]->Table->TableName);
        }
        m_instance.bandsetTabNameList->Add(bandset);
        System::Diagnostics::Debug::WriteLine("After");
        System::Diagnostics::Debug::WriteLine(m_instance.bandsetTabNameList->Count);
        for (int index = 0; index < m_instance.bandsetTabNameList->Count; index++)
        {
            System::Diagnostics::Debug::WriteLine(index);
            System::Diagnostics::Debug::WriteLine(m_instance.bandsetTabNameList[index]->Name);
            System::Diagnostics::Debug::WriteLine(m_instance.bandsetTabNameList[index]->Table->TableName);
        }
    }
};

};

我从另一个类中的函数调用它们,如下所示:

SharedData ^sharedData = SharedData::Instance;
wchar_t wcs[80];
wcscpy_s(wcs, AWARD_SYMBOL_TABLE_STRING);
            wcscat_s(wcs, std::to_wstring(++sharedData->AwardSymbolTableIndex).c_str());
sharedData->AddBandsetTab(gcnew String(wcs));

如果我打电话一次,那么列表内容是:

  • bandSetTable1

如果我第二次打电话,那么内容就变成了:

  • bandSetTable2
  • bandSetTable2

我不知道这是什么问题。我是C ++ / CLI的新手。

有人可以指出这个问题吗。

感谢。

1 个答案:

答案 0 :(得分:0)

标准警告:虽然可以用C ++ / CLI编写应用程序的主体,甚至可以使用WinForms在C ++ / CLI中编写GUI,但不建议这样做。 C ++ / CLI适用于互操作场景:C#或其他.Net代码需要与非托管C ++接口,C ++ / CLI可以提供两者之间的转换。因此,C ++ / CLI具有C ++的所有复杂性,C#的所有复杂性以及它自身的一些复杂性。对于主要开发,如果您需要托管代码,建议将C#与WinForms或WPF一起使用,如果您想要非托管,则建议使用带有MFC的C ++。

public ref class Bandset
{
private:
    static String ^bandsetTabName;
    static DataTable ^bandsetTable;

public:
    static property String^ Name { String^ get() { return bandsetTabName; } void set(String ^ value) { bandsetTabName = value; }};
}

您可以静态定义这两个属性。这意味着只有一个名称和一个数据表,这两个名称都由所有波段集共享。

void SharedData::AddBandsetTab(String ^name)
{
    Bandset^ bandset;// = gcnew Bandset();

    bandset->Name = name;

所以在这里,bandsetnullptr,因为你没有初始化它。 bandset->Name未抛出NullReferenceException的唯一原因是Name是静态属性,因此实例的值(当前为nullptr)不会用于所有。

    m_instance.bandsetTabNameList->Add(bandset);

这只是将nullptr插入到列表中。

    Debug::WriteLine(m_instance.bandsetTabNameList[index]->Name);

同样,由于Name是一个静态属性,m_instance.bandsetTabNameList[index]nullptr的事实并不重要。