6 Replies - 555 Views - Last Post: 03 October 2012 - 03:31 PM Rate Topic: -----

#1 risky14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 12-September 12

subtract a pre-existing number from a users number

Posted 03 October 2012 - 01:41 PM

I honestly dont know how there are experts out there in programming coz every single bit of code I come across always has a problem with it and tonight is no exception.

I want to subtract a number from my sql server database from a number in a textbox. The number in the textbox is the number the user has typed in.
Below is the code I want to use to subtract the 2 numbers. What do I need to put after the equals sign in order to allow the user to input whatever number they want into the textbox?

private void btnSubtract_Click(object sender, EventArgs e)
        {
            int answerSubtract;
            int numberOne = 6;

            answerSubtract =  - numberOne;
            
            MessageBox.Show(answerSubtract.ToString());
        }


Also does anyone know of a better site than Home and Learn?

This post has been edited by modi123_1: 03 October 2012 - 01:47 PM
Reason for edit:: highlight the lines THEN click the code tags button in the 'format text' box of the post.


Is This A Good Question/Topic? 0
  • +

Replies To: subtract a pre-existing number from a users number

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9570
  • View blog
  • Posts: 36,240
  • Joined: 12-June 08

Re: subtract a pre-existing number from a users number

Posted 03 October 2012 - 01:51 PM

This line:
// what are you doing in this line?  You are telling "answerSubtract" to get the value of numberOne but with a negative sign.
answerSubtract =  - numberOne;

// I you mean this.. which is the equivolent of  answerSubtract = answerSubtract - numberOne;
answerSubtract -= numberOne;




Where's your database connection and all that jazz that goes with jumping out to your db?
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: subtract a pre-existing number from a users number

Posted 03 October 2012 - 01:55 PM

Also keep in mind that since answerSubtract is defined as an integer, it can only hold whole integer values. Thus if you want to let the user enter in things like 1.25 then do realize you are not going to get a decimal in your answer variable.

In addition, again being an integer, it has certain limits on the values it can hold. For instance if the user wants to subtract 3 billion, the answer may not be what you expect it to be.

:)
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: subtract a pre-existing number from a users number

Posted 03 October 2012 - 02:04 PM

The "number" in the textbox isn't a number, it is a string.

And cos it's a string, you need to see if it parse-ible to a integer.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,190
  • Joined: 02-June 10

Re: subtract a pre-existing number from a users number

Posted 03 October 2012 - 02:32 PM

Quote

I honestly dont know how there are experts out there in programming coz every single bit of code I come across always has a problem with it and tonight is no exception.


Asking for help from experts, then starting off by implying that nobody including those that call themselves experts can write a decent bit of code, may not be the approach you want to use.

Then consider what you are saying... 'this code I came across'... Which immediately makes me think "Why does this guy think an expert wrote this? Its probably crap from another student just as lost as he his." Its code you found. Don't assume that it came from an expert, and don't lump all experts together anymore than you would lump all doctors as the same skill level - we all know there are good and bad of every profession.

'this code I came across'... So in other words you're not even bothering to try to write your own code. You're just trolling the 'net looking for something that someone else wrote that you can plagiarize. What a wonderful approach to your homework and your education that presumably is geared toward a career. Is it your intent to continue this practice in the work place? To not actually write code for projects you're assigned, but instead hope to find ready made solutions you can claim as your own?

Everyone else has pointed out the numerous problems with your code, some of which like the -= instead of just -, I will assume are transcription errors.

I just wanted to point out that your biggest problem here is that you didn't even try to write your own code. If you don't want to make an effort then drop the course and free up the space from someone that genuinely wants to be there.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,190
  • Joined: 02-June 10

Re: subtract a pre-existing number from a users number

Posted 03 October 2012 - 03:14 PM

Quote

Also does anyone know of a better site than Home and Learn?

What is 'home and learn'? Its meaningless to us if you don't give a link to the actual site. For all we know its some rookie's blog.

'Better' is a subjective term determined by need. Which is better: A VW beetle or an 18 wheeler? Depends on your needs and circumstances. So 'better' for what, to provide what?
Was This Post Helpful? 0
  • +
  • -

#7 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: subtract a pre-existing number from a users number

Posted 03 October 2012 - 03:31 PM

So what you're saying is that the site you're using, full of FREE tutorials, walk-throughs, etc, isn't up to your standards? You could always buy a book and work through it, like most of the rest of us did.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1