Winforms Windows窗体C++中自己类对象的全局列表
好的,首先我想说,我的英语不是很好:D 我在VS2013中编写一个项目,在C++中,Windows窗体。 我有三张表格和一个自己的班级 我不想将文本从textboxesString^转换为标准字符串,因此我已经完成了一个公共ref类:Winforms Windows窗体C++中自己类对象的全局列表,winforms,list,c++-cli,Winforms,List,C++ Cli,好的,首先我想说,我的英语不是很好:D 我在VS2013中编写一个项目,在C++中,Windows窗体。 我有三张表格和一个自己的班级 我不想将文本从textboxesString^转换为标准字符串,因此我已经完成了一个公共ref类: public ref class Employee { String^ FirstName; String^ Surname; String^ IDnumber; int Payment; int Age; bool
public ref class Employee
{
String^ FirstName;
String^ Surname;
String^ IDnumber;
int Payment;
int Age;
bool Sex;
String^ Phone;
String^ Email;
List<String^>^ Regions;
List<String^>^ Skills;
String^ Profession;
public:
Employee(String^ FirstName, String^ Surname, String^ IDnumber, int Payment, int Age, bool Sex, String^ Phone, String^ Email, List<String^>^ Regions, List<String^>^ Skills, String^ Profession);
Employee();
};
对于EmployeeCreator类中的VS2013,主窗口不是一种类型:
失踪;在^之前
如果我包括MainWindow.h,那么包含的文件太多了。下面是一个使用Employee类中静态员工列表的示例,它还详细介绍了在引用列表时使用list^
public ref class Employee {
static List<Employee^>^ AllEmployees;
void AddToAllEmployees()
{
if (!AllEmployees)
AllEmployees = gcnew List<Employee^>();
AllEmployees->Add(this);
}
public:
System::String^ Firstname;
System::String^ Surname;
Employee(System::String ^firstname, System::String^ surname)
{
Firstname = firstname;
Surname = surname;
AddToAllEmployees();
};
Employee() {
Firstname = "";
Surname = "";
AddToAllEmployees();
}
~Employee() {
if (AllEmployees)
AllEmployees->Remove(this);
};
static List<Employee^>^ GetAllEmployees() { return AllEmployees; }
};
void testEmployeeClass() {
Employee^ johnDoe = gcnew Employee("John", "Doe");
Employee^ olaNordmann = gcnew Employee("Ola", "Nordmann");
Employee^ nn = gcnew Employee();
List<Employee^>^ everybody = Employee::GetAllEmployees();
for each (Employee^ tmp in everybody) {
// Do something _useful_ with the employees
System::Diagnostics::Debug::WriteLine("Employee: " + tmp->Firstname +" " + tmp->Surname);
}
}
“我的测试函数”将输出显示到Visual Studio 2013的调试窗口,但您肯定有更合理的方法来处理该列表。此外,我将Employee类精简到最低限度,并将成员移出公共部分,以使列表处理更加突出
问候
甚至
编辑:在问题的后半部分,您提到的主窗口不是类型错误,只是想告诉您一个小秘密:VisualStudio和大多数编译器提供的错误消息通常指的是前面的错误。也就是说,错误与MainWindow.h无关,而是与列出的错误行前面的某个内容有关。。。不仅要查看错误行,还要查看报告错误行前面的行 列表是引用类型,因此必须使用^hat声明变量。所以它是列表^列表;现在出现以下错误:具有静态存储持续时间的变量不能具有句柄或跟踪引用类型。请将其设置为类的成员,而不是全局变量。如有必要,将其声明为静态。
#include "Employee.h"
#include "EmployeeCreator.h"
#include "OfferCreator.h"
#pragma once
namespace EmploymentAgency {
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::Collections::Generic;
public ref class MainWindow : public System::Windows::Forms::Form
{
public:
MainWindow(void)
{
InitializeComponent();
//
//TODO: W tym miejscu dodaj kod konstruktora
//
}
public: List<Employee^> lista;
....
#pragma once
namespace EmploymentAgency {
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 EmploymentAgency;
using namespace System::Collections::Generic;
/// <summary>
/// Podsumowanie informacji o EmployeeCreator
/// </summary>
public ref class EmployeeCreator : public System::Windows::Forms::Form
{
public:
EmployeeCreator(void)
{
InitializeComponent();
//
//TODO: W tym miejscu dodaj kod konstruktora
//
}
public: MainWindow^ main;
private: System::Void AddEmployee_Click(System::Object^ sender, System::EventArgs^ e) {
EmployeeCreator ^window = gcnew EmployeeCreator();
window.main = this; //it doesn't work
window->ShowDialog();
}
public ref class Employee {
static List<Employee^>^ AllEmployees;
void AddToAllEmployees()
{
if (!AllEmployees)
AllEmployees = gcnew List<Employee^>();
AllEmployees->Add(this);
}
public:
System::String^ Firstname;
System::String^ Surname;
Employee(System::String ^firstname, System::String^ surname)
{
Firstname = firstname;
Surname = surname;
AddToAllEmployees();
};
Employee() {
Firstname = "";
Surname = "";
AddToAllEmployees();
}
~Employee() {
if (AllEmployees)
AllEmployees->Remove(this);
};
static List<Employee^>^ GetAllEmployees() { return AllEmployees; }
};
void testEmployeeClass() {
Employee^ johnDoe = gcnew Employee("John", "Doe");
Employee^ olaNordmann = gcnew Employee("Ola", "Nordmann");
Employee^ nn = gcnew Employee();
List<Employee^>^ everybody = Employee::GetAllEmployees();
for each (Employee^ tmp in everybody) {
// Do something _useful_ with the employees
System::Diagnostics::Debug::WriteLine("Employee: " + tmp->Firstname +" " + tmp->Surname);
}
}