Passing an ArrayList by Reference? [modified]
-
Hi guys, I'm having a small problem, and I'd like to know what you guys think. User.h:
void User::getJobs(ArrayList^ jobs)
{
jobs = this->jobs;
}UserForm.h:
UserForm(User^ currentUser)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
this->user = gcnew User();
this->allJobs = gcnew ArrayList();Job^ currentJob = gcnew Job(); ListViewItem^ item = gcnew ListViewItem(); this->user = \*currentUser; **_this->user->getJobs(this->allJobs);_** for(int i = 0; i < allJobs->Count; i++) { currentJob = (Job^)allJobs\[i\]; String^ temp = ""; currentJob->getCustomerName(temp); item->SubItems->Add(temp); temp = ""; currentJob->getProjectName(temp); item->SubItems->Add(temp); Job::EN\_PRIORITY^ priority = Job::EN\_PRIORITY::EN\_PRIORITY\_0; currentJob->getPriority(priority); switch(\*priority) { case Job::EN\_PRIORITY::EN\_PRIORITY\_0: item->SubItems->Add("NONE"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_1: item->SubItems->Add("1"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_2: item->SubItems->Add("2"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_3: item->SubItems->Add("3"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_4: item->SubItems->Add("4"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_5: item->SubItems->Add("5"); break; default: item->SubItems->Add("NONE"); break; } listViewAllJobs->Items->Add(item); //add time stuff once I figure it out } }
I can see that when getJobs() completes job in Job.h equals the values I'm trying to retrieve, but once the function returns, this->allJobs is blank. Any ideas?
[Insert Witty Sig Here]
modified on Friday, May 13, 2011 8:54 AM
-
Hi guys, I'm having a small problem, and I'd like to know what you guys think. User.h:
void User::getJobs(ArrayList^ jobs)
{
jobs = this->jobs;
}UserForm.h:
UserForm(User^ currentUser)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
this->user = gcnew User();
this->allJobs = gcnew ArrayList();Job^ currentJob = gcnew Job(); ListViewItem^ item = gcnew ListViewItem(); this->user = \*currentUser; **_this->user->getJobs(this->allJobs);_** for(int i = 0; i < allJobs->Count; i++) { currentJob = (Job^)allJobs\[i\]; String^ temp = ""; currentJob->getCustomerName(temp); item->SubItems->Add(temp); temp = ""; currentJob->getProjectName(temp); item->SubItems->Add(temp); Job::EN\_PRIORITY^ priority = Job::EN\_PRIORITY::EN\_PRIORITY\_0; currentJob->getPriority(priority); switch(\*priority) { case Job::EN\_PRIORITY::EN\_PRIORITY\_0: item->SubItems->Add("NONE"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_1: item->SubItems->Add("1"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_2: item->SubItems->Add("2"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_3: item->SubItems->Add("3"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_4: item->SubItems->Add("4"); break; case Job::EN\_PRIORITY::EN\_PRIORITY\_5: item->SubItems->Add("5"); break; default: item->SubItems->Add("NONE"); break; } listViewAllJobs->Items->Add(item); //add time stuff once I figure it out } }
I can see that when getJobs() completes job in Job.h equals the values I'm trying to retrieve, but once the function returns, this->allJobs is blank. Any ideas?
[Insert Witty Sig Here]
modified on Friday, May 13, 2011 8:54 AM
If you are expecting getJobs() to change the incoming 'jobs' parameter to reference a different ArrayList even after the function returns, you'll need to use a "tracking reference". To do that use the % decoration and declare the function like this:
void User::getJobs(ArrayList^% jobs)
{
jobs = this->jobs;
}An alternative would be to loop and copy all the items from this->jobs into jobs. John
-
If you are expecting getJobs() to change the incoming 'jobs' parameter to reference a different ArrayList even after the function returns, you'll need to use a "tracking reference". To do that use the % decoration and declare the function like this:
void User::getJobs(ArrayList^% jobs)
{
jobs = this->jobs;
}An alternative would be to loop and copy all the items from this->jobs into jobs. John
That did the trick! I'm new to managed code and c++/CLI. All of these new operators are throwing me for a loop. Darn Windows forms projects!
[Insert Witty Sig Here]