【发布时间】: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