C++/CLI collection issues

I can't for the life of me find an example of a "Managed" form of collection in C++ that allows me to store a collection of created class objects.

I have a simple class. Vehicle. A user fills out a winform that I have created that they enter the Name of the Vehicle in one Textbox and the miles in another Textbox. They press a button... magic. Nope, b/c I'm too stupid to figure out vectors and C++/ managed requirements.

I created a class VehicleCollection. It has std::vector<Vehicle> vehicleList. I have an add function, remove function, a find function, a return Vehicle object function, etc...

I can only create "VehicleCollection" objects in the code where the button is pushed. This of course goes out of scope and I'm stuck. I try initialize above and it tells me I can't (managed class un-managed variable... blah blah blah).

With a string I got around this by:
String ^ vName;

doing whatever that "^" does worked. I can't however do that for some reason with my class.

I have been working on code for 18+ hours non-stop.

What the heck is the deal with managed and non-managed. I just want to make a collection of my vehicle objects to use later across all form objects. I can't figure out List and Collections b/c I don't understand namespace b/c we stopped at ch7 for this class and the book said they would explain in in ch23.

I just want a global variable of a collection of Vehicle. I keep clicking the "Code" button to put my code in but it isn't doing anything. What else today? I had to go to Chrome to get this to work... yep Edge (thanks).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Form Class- Mostly generated by VS
// No matter how hard I try... I can't work with a managed class.  Un-Managed vs Managed + Pointers = C++ Nightmares!

#pragma once

#include "Vehicle.h"
#include "VehicleCollection.h"
#include <vector>
#include <msclr\marshal_cppstd.h>

namespace cppVehicleMaintenanceLog {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}

// Removed a bunch of form stuff...


	private:
		// My added variables
		//
		String ^ vName;
		Double ^ vMiles;
		String ^ mName;
		String ^ mAction;

  // I need a Collection Variable Here
		// Here <vector> nope...

		// Not my code
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
// Removed auto generated stuff
#pragma endregion

// Events and my functions

// New Vehicle Menu Item
private: System::Void vehicleToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
	this->pNewVehicle->Show();
	this->pMaintenanceViewer->Hide();
	this->pWelcome->Hide();
	this->bSaveNewVehicle->Enabled = true;
	this->bSaveNewVehicle->Show();
	this->lbNewVehicleList->Enabled = false;
}

// New Maintenance Action
private: System::Void maintenanceActionToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
	this->lblVehicleLoaded->Text = L"" + vName;
	this->pMaintenanceViewer->Show();
	this->pNewVehicle->Hide();
	this->pWelcome->Hide();
}

// Menu Item for loading a vehicle (not creating a new one)
private: System::Void vehicleToolStripMenuItem1_Click(System::Object^  sender, System::EventArgs^  e) {
	this->lbNewVehicleList->Enabled = true;
	this->pNewVehicle->Show();
	this->pMaintenanceViewer->Hide();
	this->pWelcome->Hide();
	this->bSaveNewVehicle->Enabled = false;
	this->bSaveNewVehicle->Hide();

}

// Exit Program
private: System::Void exitProgramToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
	//  Closes the program
	Application::Exit();

}

// Button Press for Save New Vehicle
private: System::Void bSaveNewVehicle_Click(System::Object^  sender, System::EventArgs^  e) {
	// Overall, working in C++ with System::<anything> and std::<anything> is rather confusing.  I hate it and this was my attempt to do right by it.
	// I spend over 11 hours tring to figure out this part
	// The order of what is taught by Deitel is way off for c++
	// Chapter 2 pg. 42- "Namespaces are an advanced C++ feature that we discuss in depth in Chapter 23"
	// They skip talking about one of the most important parts of understanding C programming.  WHY?

	// 3 variables for name, due to not understanding pointers, managed, un-managed.
	vName = tbNewVehicle->Text; // Managed variable vName.
	System::String^ tempVehicleName = vName; // I'm not sure why I needed to do this, but I tried vName in the next line and it failed.
	std::string stdVehicleName = msclr::interop::marshal_as< std::string >(tempVehicleName); // finally a string that I can work with.
	
	// 3 variables for miles, due to not being able to cast a System::String
	String^ tempMilesString = tbNewVehicleMiles->Text;
	double tempMilesDouble = (float)(Convert::ToDouble(tempMilesString));
	vMiles = tempMilesDouble;  // Back to a managed variable
	
	// I tried declairing vList elsewhere but I couldn't figure it out.  Managed vs Un-Managed, can't figure out global public variables.
	// I simply don't know how to reference global variables from elsewhere (maybe just the managed / unmanaged issue).  So static it is.
	// Of course I wish to use this list in other places...
	// I have spent over 10 hours now trying to get a vector list of my objects to work in this crappy managed class, I just don't get it.
	static VehicleCollection vList;

	// Create a vehicle object
	Vehicle newVehicle(stdVehicleName, tempMilesDouble);

	// Now I test if the vehicle is already in the collection
	if (!vList.vehicleExists(stdVehicleName)) {
		vList.addVehicle(newVehicle);
		MessageBox::Show("Vehicle: " + vName + " Miles: " + tempMilesString + " has been added!");
		this->lbNewVehicleList->Items->Add(vName);
		this->tbNewVehicle->Text = L"";
		this->tbNewVehicleMiles->Text = L"";
	}
	else {
		MessageBox::Show("Vehicle: " + vName + " Miles: " + tempMilesString + " already existed and wasn't added!");
	}

	// Spare code, must keep.  Took me 8 hours to figure this all out.

	//newVehicle.setVehicleName("New Name- Mazda");
	//
	//vName = gcnew String(newVehicle.getVehicleName().c_str());
	//MessageBox::Show(vName);
	//
	//
	//newVehicle = vList.findVehicle("Mazda");
	//vName = gcnew String(newVehicle.getVehicleName().c_str());
	//tempVehicleName = vName;
	//stdVehicleName = msclr::interop::marshal_as< std::string >(tempVehicleName);
	//MessageBox::Show(vName);


}

// Managing the NewVehicleList on the pNewVehicle
private: System::Void lbNewVehicleList_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {

// I lose my mind here.  Because I need a global vehicle list.

	 static VehicleCollection vList;  // A static vehicle list

	vName = MyForm::lbNewVehicleList->Text;
	System::String^ tempVehicleName = vName; // I'm not sure why I needed to do this, but I tried vName in the next line and it failed.
	std::string stdVehicleName = msclr::interop::marshal_as< std::string >(tempVehicleName); // finally a string that I can work with.
	this->tbNewVehicle->Text = L"" + vName;
	MessageBox::Show("" + vList.vehicleExists(stdVehicleName));

// this part fails as the list is always empty... 

	if (vList.vehicleExists(stdVehicleName)) {
		Vehicle thisVehicle;
		thisVehicle = vList.findVehicle(stdVehicleName);
		this->tbNewVehicleMiles->Text = L"" + thisVehicle.getVehicleCurrentMiles();
	}
}



};
}
Last edited on
I can only create "VehicleCollection" objects in the code where the button is pushed. This of course goes out of scope and I'm stuck. I try initialize above and it tells me I can't (managed class un-managed variable... blah blah blah).


Normal procedure is to have the "VehicleCollection" as a member variable in the form and create in in the constructor.
1
2
3
4
5
6
7
MyForm(void)
{
  InitializeComponent();
	
  //TODO: Add the constructor code here
	
}


In the destructor you delete it.
1
2
3
4
5
6
7
8
9
10
11
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
  // delete your VehicleCollection if neccessary
  if (components)
  {
	delete components;
  }
}


I can only create "VehicleCollection" objects in the code where the button is pushed. This of course goes out of scope and I'm stuck. I try initialize above and it tells me I can't (managed class un-managed variable... blah blah blah).


When the button is clicked you create your vehicle and add it to the VehicleCollection. If the VehicleCollection is a member of the form it doesn't go out of scope until the form is destroyed.

On code CodeProject you can find some tutorials about C++/CLI or "Managed C++" as it was formerly called.
http://www.codeproject.com/search.aspx?q=c%2B%2B+cli&x=0&y=0&sbo=kw

Topic archived. No new replies allowed.