Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.25/5 (4 votes)
hi, i have been working on creating a payslip form where users can input how many hours they work a week, how much they earn an hour and i want to make the program work out how much they earn before and after taxes.

but i am having problems with getting a number value from the hours textbox and hourlyrate textbox and then displaying it in a the answer label the following code i have got is:

txt_hours.Text = hours.ToString();
hours = Convert.ToInt32(txt_hours.Text);
hours = int.Parse(txt_hours.Text);


txt_hourlyr.Text = hourlyrate.ToString();
hourlyrate = Convert.ToInt32(txt_hourlyr.Text);
hourlyrate = int.Parse(txt_hourlyr.Text);

answer = Convert.ToString(hours * hourlyrate);

answer = Convert.ToString(hours * hourlyrate);


can anyone help me please



UPDATE:

i have now scraped that code as i was going into the wrong direction, but the problem i have now is the "answer = hours * hourlyrate;" does not implicity convert type int to string

int hours;
int hourlyrate;
string answer;

hours = int.Parse(txt_hours.Text);
hourlyrate = int.Parse(txt_hourlyr.Text);

answer = hours * hourlyrate;
lbl_weeklypay.Text = answer.ToString();</pre>
Posted
Updated 19-Mar-15 8:08am
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-15 13:48pm    
Where is the declaration of hours?
—SA
ZurdoDev 19-Mar-15 13:56pm    
What is your question?
Member 11539357 19-Mar-15 13:58pm    
my question is it doesnt work, it doesnt seem to get the value the user input in the text box and times it and just display a 0 as answer
Sergey Alexandrovich Kryukov 19-Mar-15 14:00pm    
There is nothing to work... And you did nit explain what you are trying to do. Please see Solution 1.
—SA

Oh… It all looks like real gibberish. Look what you are doing. In first line, you assume that you have some value, hours, probably of the type int. In next line, you take the string you obtained, by some weird reason, from Text property, and convert it back to int. It makes no sense, you made a round trip and, at best, could get the same value. As if it wasn't enough, you convert the string to hours once again, so both the initial hours value and the value obtained in the previous line are discarded. Very nice. How about elementary logic?

Also note that int.Parse can throw and exception (but not in the case of your pointless round trip). We cannot see if you handle this exception in right place, but you are advised to use int.TryParse instead. This is how you parse a string into any numeric value; and the opposite operation would be ToString.

I think, this is enough; the lines below are no better. I already explained what to do, but to really help you, I would advise: stop doing what you are doing. Hold on with UI and other advanced icons. Grab some manual on language, platform and general programming and read it all, from the very beginning, doing simplest possible exercises as you go, better some simple console-only applications and some class library assemblies (always use them and test usage). Only when you feel quite comfortable with that, make another round, this time getting to a bit more advanced topis. And so on…

—SA
 
Share this answer
 
Comments
Member 11539357 19-Mar-15 14:13pm    
first time using windows form application:

i have scraped the code before as like you say goes round and tried another way
int hours;
int hourlyrate;
string answer;
hours = int.Parse(txt_hours.Text);
hourlyrate = int.Parse(txt_hourlyr.Text);
answer = hours * hourlyrate;

but the last part "answer" says cannot convert to string im not sure how to solve this
Sergey Alexandrovich Kryukov 19-Mar-15 14:41pm    
Don't use int.Parse, use int.TryParse;

Do this:

int answer = hours*hourlyrate;
string answerString = answer.ToString();

Once again: I answer in overly detailed manner, but yet, you are not grasping the idea and ask further questions. Better admit it: you did not yet have a clue how to do general programming, not just UI, so you need to learn it. Please start learning it, stop wasting your and our time.

—SA
Member 11539357 19-Mar-15 15:29pm    
already tried "int.tryparse" it displays message saying "no overload for method'tryparse' takes 1 arguments
Sergey Alexandrovich Kryukov 19-Mar-15 19:43pm    
Are you serious? Are you trying to just change only the name not arguments?! Without looking at Intellisense, set aside reading the help page? First time in my life I observe such a weird action. I can only repeat that you should stop doing what you doing and learn something. Any help for you goes nowhere.
—SA
BillWoodruff 20-Mar-15 4:50am    
My vote of #2: This "lecture" has some good points, but is not a solution, and is "contaminated" by unnecessary remarks of a personal nature to the OP.
To fix the problem with "does not implicity convert type int to string " just change the type of answer to int.

Consider also use float, double or decimal instead to support non-integer input.

... and use TryParse to check for non-numeric input.
 
Share this answer
 
Comments
Member 11539357 19-Mar-15 15:39pm    
already tried "int.tryparse" it displays message saying "no overload for method'tryparse' takes 1 arguments
Vamshi Krishna Naidu 19-Mar-15 16:18pm    
Whoa My Friend, TryParse Requires two arguments. Not One. See Below.

int number;
bool result = Int32.TryParse(value, out number);

value is your string change result to whatever format you want.(In this case int32)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900