Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Adding two numbers together?

Adding two numbers together?

Scheduled Pinned Locked Moved Visual Basic
helpquestion
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MallardsReach

    I am trying to make a simple score card by entering the score for each player for each round in a lbl_round.text and then having each round show in a Lbl_total.text Problem is, if player 1 in round 1 scores 7 all is fine round1.text shows 7 and total.text shows 7 but when I come again to player 1, round score may be 6 which should show in total score as 13 but instead I get 76. Below is the code:

        Static counter = 0
       Dim labels = {Lbl\_roundscore1, Lbl\_roundscore2, Lbl\_roundscore3, Lbl\_roundscore4}
       Dim totals = {Lbl\_total1, Lbl\_total2, Lbl\_total3, Lbl\_total4}
       labels(counter).Text = Txt\_input.Text
       totals(counter).Text = totals(counter).Text + labels(counter).Text
       Txt\_input.Text = String.Empty
       counter = If(counter = labels.Count - 1, 0, counter + 1)
    
    R Offline
    R Offline
    Ralf Meier
    wrote on last edited by
    #2

    If you "add" strings then you really expand them - that means : the Result of adding "7" and "6" is "76". That is what your code does. What you want to have is adding the numeric values - so you should cast each string perhaps to an Integer and add them now. Finally you convert the Integer-Value back into your Result ...

    Richard DeemingR M 2 Replies Last reply
    0
    • R Ralf Meier

      If you "add" strings then you really expand them - that means : the Result of adding "7" and "6" is "76". That is what your code does. What you want to have is adding the numeric values - so you should cast each string perhaps to an Integer and add them now. Finally you convert the Integer-Value back into your Result ...

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #3

      Ralf Meier wrote:

      you should cast each string perhaps to an Integer

      You can't "cast" a string to an integer; you need to parse it. :) Int32.TryParse[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      R 1 Reply Last reply
      0
      • M MallardsReach

        I am trying to make a simple score card by entering the score for each player for each round in a lbl_round.text and then having each round show in a Lbl_total.text Problem is, if player 1 in round 1 scores 7 all is fine round1.text shows 7 and total.text shows 7 but when I come again to player 1, round score may be 6 which should show in total score as 13 but instead I get 76. Below is the code:

            Static counter = 0
           Dim labels = {Lbl\_roundscore1, Lbl\_roundscore2, Lbl\_roundscore3, Lbl\_roundscore4}
           Dim totals = {Lbl\_total1, Lbl\_total2, Lbl\_total3, Lbl\_total4}
           labels(counter).Text = Txt\_input.Text
           totals(counter).Text = totals(counter).Text + labels(counter).Text
           Txt\_input.Text = String.Empty
           counter = If(counter = labels.Count - 1, 0, counter + 1)
        
        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #4

        This is the kind of problem you get when using controls to store your game state. DON'T DO THAT! Controls are there to SHOW and EDIT your data, not to hold it for you. Maintain your values (state) in a class, or other appropriate storage, such as a structure, designed for the purpose. The controls can then get their values from the game state storage. Doing this also frees you from parsing or casting data back and forth.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        M 2 Replies Last reply
        0
        • R Ralf Meier

          If you "add" strings then you really expand them - that means : the Result of adding "7" and "6" is "76". That is what your code does. What you want to have is adding the numeric values - so you should cast each string perhaps to an Integer and add them now. Finally you convert the Integer-Value back into your Result ...

          M Offline
          M Offline
          MallardsReach
          wrote on last edited by
          #5

          Thanks Ralf for taking the time to answer will look in to what you suggest.

          1 Reply Last reply
          0
          • D Dave Kreskowiak

            This is the kind of problem you get when using controls to store your game state. DON'T DO THAT! Controls are there to SHOW and EDIT your data, not to hold it for you. Maintain your values (state) in a class, or other appropriate storage, such as a structure, designed for the purpose. The controls can then get their values from the game state storage. Doing this also frees you from parsing or casting data back and forth.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            M Offline
            M Offline
            MallardsReach
            wrote on last edited by
            #6

            Well excuse me Dave for being the fool, only just started with VB so will go away and try to decipher what it is you are trying to tell me.

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              This is the kind of problem you get when using controls to store your game state. DON'T DO THAT! Controls are there to SHOW and EDIT your data, not to hold it for you. Maintain your values (state) in a class, or other appropriate storage, such as a structure, designed for the purpose. The controls can then get their values from the game state storage. Doing this also frees you from parsing or casting data back and forth.

              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
              Dave Kreskowiak

              M Offline
              M Offline
              MallardsReach
              wrote on last edited by
              #7

              Well here's what I came up with after reading some tutorials it works so I'm happy but is it correct?

              Static counter = 0
              Dim labels = {Lbl_roundscore1, Lbl_roundscore2, Lbl_roundscore3, Lbl_roundscore4}
              Dim totals = {Lbl_total1, Lbl_total2, Lbl_total3, Lbl_total4}
              labels(counter).Text = Txt_input.Text
              Dim num1 As Single = Val(labels(counter).Text), num2 As Single = Val(totals(counter).Text)
              Dim Sum As Single = num1 + num2
              totals(counter).Text = Sum.ToString
              Txt_input.Text = String.Empty
              counter = If(counter = labels.Count - 1, 0, counter + 1)

              L 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Ralf Meier wrote:

                you should cast each string perhaps to an Integer

                You can't "cast" a string to an integer; you need to parse it. :) Int32.TryParse[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                R Offline
                R Offline
                Ralf Meier
                wrote on last edited by
                #8

                :omg: oO ... I wrote cast and I meant convert - but I think it's clear what I tried to say ... ;)

                1 Reply Last reply
                0
                • M MallardsReach

                  Well here's what I came up with after reading some tutorials it works so I'm happy but is it correct?

                  Static counter = 0
                  Dim labels = {Lbl_roundscore1, Lbl_roundscore2, Lbl_roundscore3, Lbl_roundscore4}
                  Dim totals = {Lbl_total1, Lbl_total2, Lbl_total3, Lbl_total4}
                  labels(counter).Text = Txt_input.Text
                  Dim num1 As Single = Val(labels(counter).Text), num2 As Single = Val(totals(counter).Text)
                  Dim Sum As Single = num1 + num2
                  totals(counter).Text = Sum.ToString
                  Txt_input.Text = String.Empty
                  counter = If(counter = labels.Count - 1, 0, counter + 1)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  "Dim Sum is Single". Poor Dim Sum :D How would you define "correct"? If it works, it works. It's not nicely parsing and displaying error-messages if someone enters a text, or Roman numerals.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    "Dim Sum is Single". Poor Dim Sum :D How would you define "correct"? If it works, it works. It's not nicely parsing and displaying error-messages if someone enters a text, or Roman numerals.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    M Offline
                    M Offline
                    MallardsReach
                    wrote on last edited by
                    #10

                    Hi Eddy, Yes it works and not displaying error messages if you enter text or roman numerals, so for me that's OK. By correct I guess I mean is it good coding or is any code good if it works?

                    L 1 Reply Last reply
                    0
                    • M MallardsReach

                      Hi Eddy, Yes it works and not displaying error messages if you enter text or roman numerals, so for me that's OK. By correct I guess I mean is it good coding or is any code good if it works?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #11

                      "Good" is in the eyes of the beholder; I prefer to see error-handling when getting user-input. If you don't mind, who am I to say it is wrong? Are you sure that "static" is what you want, or do you just need some int? And again, you're using the UI to keep a current "state"; meaning, you're abusing the controls for something that should be a separate variable. ..and no documentation. No single comment. I'd be more impressed if the amount of totals were variable, but impossible to say if that's logical from a user-viewpoint. Depends on what the code is required to do.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                      M 1 Reply Last reply
                      0
                      • L Lost User

                        "Good" is in the eyes of the beholder; I prefer to see error-handling when getting user-input. If you don't mind, who am I to say it is wrong? Are you sure that "static" is what you want, or do you just need some int? And again, you're using the UI to keep a current "state"; meaning, you're abusing the controls for something that should be a separate variable. ..and no documentation. No single comment. I'd be more impressed if the amount of totals were variable, but impossible to say if that's logical from a user-viewpoint. Depends on what the code is required to do.

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                        M Offline
                        M Offline
                        MallardsReach
                        wrote on last edited by
                        #12

                        Are you sure that "static" is what you want, or do you just need some int? Not sure what I want only started using it 2 days ago. The program is only for me when playing a game with family that scores each round and has a running total. It's so small it doesn't need comments so is it good? It does what I want without errors it's for personal use so I guess it's good. Thanks to all who have contributed.

                        L 1 Reply Last reply
                        0
                        • M MallardsReach

                          Are you sure that "static" is what you want, or do you just need some int? Not sure what I want only started using it 2 days ago. The program is only for me when playing a game with family that scores each round and has a running total. It's so small it doesn't need comments so is it good? It does what I want without errors it's for personal use so I guess it's good. Thanks to all who have contributed.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          MallardsReach wrote:

                          It does what I want without errors it's for personal use so I guess it's good.

                          Then you succesfully met the demand of the customer :)

                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups