List 将文本添加到全局类的静态列表中
我正在使用一个全局类来设置其中的一些信息。我想在这门课上填一张单子 我正在尝试这个:List 将文本添加到全局类的静态列表中,list,static,c++-cli,global,List,Static,C++ Cli,Global,我正在使用一个全局类来设置其中的一些信息。我想在这门课上填一张单子 我正在尝试这个: #pragma once #include "Element.h" #include "PLCconnection.h" ref class ManagedGlobals { public: static List<Element^>^ G_elementList; //wordt gedefineerd in ReadPlotFile.cpp static System::S
#pragma once
#include "Element.h"
#include "PLCconnection.h"
ref class ManagedGlobals {
public:
static List<Element^>^ G_elementList; //wordt gedefineerd in ReadPlotFile.cpp
static System::String^ G_plotFileName; //wordt gedefineerd in PlotFileForm.h
static System::String^ G_Language; //wordt gedefineerd in MainForm.h
static PLCconnection^ G_PLCverbinding = gcnew PLCconnection();
static bool G_plcOnline = G_PLCverbinding->ConnectToPLC();
static List<System::String^>^ G_VariableList = gcnew List<System::String^>;
//static List<System::String^>^ G_VariableList = gcnew List <System::String^>;
G_VariableList = G_PLCverbinding->LeesTest2(); // this line gives the error
};
#pragma一次
#包括“Element.h”
#包括“PLCconnection.h”
ref类ManagedGlobals{
公众:
静态列表^G_elementList;//ReadPlotFile.cpp中的wordt gedefineerd
静态系统::String ^G_plotFileName;//PlotFileForm.h中的wordt gedefineerd
静态系统::String ^G_Language;//MainForm.h中的wordt gedefineerd
静态PLC连接^G_PLCverbinding=gcnew PLC连接();
静态bool G_plcOnline=G_PLCverbinding->ConnectToPLC();
静态列表^G_VariableList=gcnew列表;
//静态列表^G_VariableList=gcnew列表;
G_VariableList=G_PLCverbinding->LeesTest2();//此行给出了错误
};
我得到的错误:此声明没有存储类或类型说明符
我怎样才能解决这个问题?我在项目中的多个位置使用此列表,因此我需要它是全局的。您必须在单个表达式中声明
G\u VariableList
成员,如下所示:
static List<System::String^>^ G_VariableList = G_PLCverbinding->LeesTest2();
请记住,从静态构造函数中抛出的任何异常,或作为第一个示例中“内联”静态初始化的一部分抛出的任何异常,都将导致类型不可用,因此最好确保调用来初始化静态变量的PLCconnection
类中的方法不会抛出异常。发件人:
如果静态构造函数抛出异常,运行时将不会再次调用它,并且在程序运行的应用程序域的生命周期内,该类型将保持未初始化状态
最后,考虑静态共享数据是线程安全问题的肥沃来源,因此,如果要从不同线程读取和写入静态变量,则必须使用锁或其他同步机制来同步读/写操作。
ref class ManagedGlobals {
static ManagedGlobals()
{
PLCconnection^ plcVerbinding = gcnew PLCconnection();
G_plcOnline = plcVerbinding->ConnectToPLC();
G_VariableList = plcVerbinding->LeesTest2();
}
public:
static bool G_plcOnline;
static List<System::String^>^ G_VariableList;
};