【问题标题】:System::Collections::Generic::List of Custom class objects c++/cliSystem::Collections::Generic::自定义类对象列表 c++/cli
【发布时间】:2018-01-23 05:12:38
【问题描述】:

我正在尝试将我的自定义类“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 很陌生。

谁能指出问题。

谢谢。

【问题讨论】:

    标签: c++-cli


    【解决方案1】:

    标准警告:当然可以用 C++/CLI 编写应用程序的主体,甚至可以使用 WinForms 用 C++/CLI 编写 GUI,但不建议这样做。 C++/CLI 旨在用于互操作场景:在 C# 或其他 .Net 代码需要与非托管 C++ 交互的情况下,C++/CLI 可以提供两者之间的转换。因此,C++/CLI 具有 C++ 的所有复杂性、C# 的所有复杂性以及它自己的一些复杂性。对于初级开发,如果您想要托管代码,建议使用带有 WinForms 或 WPF 的 C#,或者如果您想要非托管代码,则建议使用带有 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-&gt;Name 没有抛出 NullReferenceException 的唯一原因是因为 Name 是静态属性,所以根本不使用实例的值(当前为 nullptr)。

        m_instance.bandsetTabNameList->Add(bandset);
    

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

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

    同样,因为Name 是一个静态属性,所以m_instance.bandsetTabNameList[index]nullptr 并不重要。

    【讨论】:

    • 对不起,我粘贴了旧代码。我实际上尝试使它们成为非静态的,那时我得到了上述输出。代码也是这样。 Bandset^ bandset = gcnew Bandset();
    • 请用您正在使用的实际代码编辑您的问题。
    • 已编辑,请立即查看。 :)
    • @DavidYaw Jeff 实习生 cspeed。这里有任何想法:stackoverflow.com/questions/48529050/…
    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多