Hello,

Can anyone help with this error?

http://img27.imageshack.us/f/errorpq.jpg/

I try and click text in the list box and it crashes.

Thanks, Matt :)

Recommended Answers

All 18 Replies

what's the value of _selectedName at that point?

what's the value of _selectedName at that point?

I've declared it as a string,

" String^ _selectedName = lstName->Items[myIndex]->ToString(); "

I've declared it as a string,

" String^ _selectedName = lstName->Items[myIndex]->ToString(); "

But what is the value of it? Mouse over it during the crash and it should tell you.

I don't know what was up with that other poster...

Hover over _selectedName, not firstName, there's something in the string that can't be converted to an integer.

All else fails, put in a line System::Diagnostics::Debug.WriteLine(_selectedName+" STRING"); and read what it says in the output window of the IDE.

Also, just as a side note, you can prevent this by using int::TryParse instead, it will return a value of false instead of throwing an exception.

I need it so that when the user clicks the text in the list box, a form comes up, like when the user clicks a number on the list box to the left.

This is the code;

private: System::Void lstName_DoubleClick(System::Object^  sender, System::EventArgs^  e) 
		 
		 {

			 int myIndex = lstName->SelectedIndex;


			 String^ _selectedName = lstName->Items[myIndex]->ToString();

			 Staff_Member^ OpenStaff = gcnew Staff_Member;
			 OpenStaff->Show();

			 OpenStaff->txtName->Text=
			 FirstName[int::Parse(_selectedName)];

			 OpenStaff->txtAddress->Text=
			 Address[int::Parse(_selectedName)];

			 OpenStaff->txtPostcode->Text=
			 PostCode[int::Parse(_selectedName)];

			 OpenStaff->txtTaxCode->Text=
			 TaxCode[int::Parse(_selectedName)];

			 OpenStaff->cmbJobTitle->Text=
			 JobTitle[int::Parse(_selectedName)];

			 OpenStaff->txtSalary->Text=
			 AnnualSalary[int::Parse(_selectedName)];

			 OpenStaff->cmbDepartment->Text=
			 Department[int::Parse(_selectedName)];

			 OpenStaff->txtPayrollNumber->Text=
			 PayrollNumber[int::Parse(_selectedName)];

		 }

};
}

Right, no I understand what you are doing, but the way you are going about it is incorrect. If _selectedName has any text in it, it messes up the int::Parse. That's why I'm asking what is in _selectedName at line 8? If it's "23 BobJones" it's not going to work, you'll have to take the substring with just "23" in it for the number to be parsed properly. If the number is not parsed properly, all of your indexes in lines 14,17,etc. are invalid, unfortunately.

please could you download TeamViewer and have a look, please?
My ID is 668 421 586 and the password is 8675

I'd prefer not to do that, let's keep it on the site. On line 9 of the last time you posted code, put in System::Diagnostics::Debug::WriteLine("****** "+_selectedName+" *******"); Run your program, now in the bottom window of the IDE (marked output) tell me what's in between the stars.

that doesn't work, it would be good if you got team viewer, connected and had a look.

this is the code for for the other list box, which you double click a number..

private: System::Void lstPayroll_DoubleClick(System::Object^  sender, System::EventArgs^  e) 
		 
		 {
			 String^ _selectedEmployeeID = lstPayroll->SelectedItem->ToString();

			 Staff_Member^ OpenStaff = gcnew Staff_Member;
			 OpenStaff->Show();

			 OpenStaff->txtName->Text=
			 FirstName[int::Parse(_selectedEmployeeID)];

			 OpenStaff->txtAddress->Text=
			 Address[int::Parse(_selectedEmployeeID)];

			 OpenStaff->txtPostcode->Text=
			 PostCode[int::Parse(_selectedEmployeeID)];

			 OpenStaff->txtTaxCode->Text=
			 TaxCode[int::Parse(_selectedEmployeeID)];

			 OpenStaff->cmbJobTitle->Text=
			 JobTitle[int::Parse(_selectedEmployeeID)];

			 OpenStaff->txtSalary->Text=
			 AnnualSalary[int::Parse(_selectedEmployeeID)];

			 OpenStaff->cmbDepartment->Text=
			 Department[int::Parse(_selectedEmployeeID)];

			 OpenStaff->txtPayrollNumber->Text=
			 PayrollNumber[int::Parse(_selectedEmployeeID)];
		 }

Yes, because if _selectedEmployeeID is a string with just a number, int::parse is not going to have any problem with it.

What do you mean "it doesn't work"? It wasn't meant to fix the problem, it's meant to indicate what is in that string at the time. It's important. It can't parse the string, you've got to figure out what is in there that is throwing int::parse off and fix it. Me logging into your system is not going to change that, plus the policy of the forum is "keep it on the site."

Then make an additional text box on your form (temporarily) to display that value. I'm really absolutely certain that's where the problem lies, but if you can't tell me what _selectedName is before those int::Parse calls, I'm not going to be able to help you. I'm not sure why you are fighting me on this, these debugging steps are a critical part of the process.

It's just a string, which I declared, .. ahh this is annoying.

It matters what's in it:

"23" -> okay, easy to get 23 out of this using parse
"23restofstring" -> can't get out what we need -> throws exception
"restofstring23" -> same deal, can't get what we need ->throws exception

If you string is either of the last two cases, parse is not going to work on it.

Until we confirm what is in the string, we can't go any further. Once we know, we can truncate it to get the number out of it and parse that. I refuse to play games, either you figure out how to get access to that value or I will not be able to help. Period.

This is the text file, which the text is dragged into the list box from.

1#Matt Whitehead#2 Real Road#BB4 5TR#5000#Executive#7000#Systems Analysis#
then the same again, however the data input will change.

The _selectedName is the 'Matt Whitehead'.

Okay, thank you. So what you need to do is select the "1" rather than the "Matt Whitehead" because there's not way for the compiler to get an array index out of "Matt Whitehead." It seems like the other routine you posted is doing that very task, though.

When you try to set:

OpenStaff->txtName->Text=FirstName[int::Parse(_selectedName)];

int::Parse is trying to extract a numerical value from "Matt Whitehead" and failing.
Even if it could parse the string, you'd be trying to take the "Matt Whitehead" element of FirstName, which does not work in C++ (it might work in Javascript or PHP).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.