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
  1. Home
  2. General Programming
  3. Visual Basic
  4. shortening my code

shortening my code

Scheduled Pinned Locked Moved Visual Basic
helpquestion
10 Posts 4 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.
  • I Offline
    I Offline
    ibok23
    wrote on last edited by
    #1

    I am needing to shortem my code up. I keep on having to repeat myself so I am trying to do subbing. I am getting an error. It is saying that I have not declared additionalguest - this is what I have Private Const additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) If nudguest.text > 2 then additionalguestcharge.text = additionalguest Could anybody tell me what I may be doing wrong? Thank you, ibok23

    N D 2 Replies Last reply
    0
    • I ibok23

      I am needing to shortem my code up. I keep on having to repeat myself so I am trying to do subbing. I am getting an error. It is saying that I have not declared additionalguest - this is what I have Private Const additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) If nudguest.text > 2 then additionalguestcharge.text = additionalguest Could anybody tell me what I may be doing wrong? Thank you, ibok23

      N Offline
      N Offline
      Nadroj
      wrote on last edited by
      #2

      ibok23 wrote: Private Const additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) where is this located? ibok23 wrote: If nudguest.text > 2 then additionalguestcharge.text = additionalguest ...where is this located? since you have declared additionalguest as a private variable, it can only be accessed in the method it is defined, unless you declare it at class/module level. if you are only using the private variable in 1 method, declare it in the same sub that the if statement is in. ..or.. is the private variable already declared in the same sub as the if statement? in this case, post more code. ------------------------ Jordan. III

      1 Reply Last reply
      0
      • I ibok23

        I am needing to shortem my code up. I keep on having to repeat myself so I am trying to do subbing. I am getting an error. It is saying that I have not declared additionalguest - this is what I have Private Const additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) If nudguest.text > 2 then additionalguestcharge.text = additionalguest Could anybody tell me what I may be doing wrong? Thank you, ibok23

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        OK. You've got a problem with the way your declaring your variables: ibok23 wrote: Private Const additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) The 'Const' means that the additionalguest variable will be a constant. Once it's defined, it can never be changed. You can't use the code in your if statement to change it. What you need to do is change the declaration of additionalguest:

        Private additionalguest As Double = ((nudguest.text - 2) \* 10) \* Val(numberofdays.text))
        

        For future reference, error messages are extremely important when asking us questions. Without the error message, we can only guess at what's wrong. Also, posting the code that you did was very helpful, keep it up! You can never post too much code! ...Well, within reason... ;P RageInTheMachine9532

        I 1 Reply Last reply
        0
        • D Dave Kreskowiak

          OK. You've got a problem with the way your declaring your variables: ibok23 wrote: Private Const additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) The 'Const' means that the additionalguest variable will be a constant. Once it's defined, it can never be changed. You can't use the code in your if statement to change it. What you need to do is change the declaration of additionalguest:

          Private additionalguest As Double = ((nudguest.text - 2) \* 10) \* Val(numberofdays.text))
          

          For future reference, error messages are extremely important when asking us questions. Without the error message, we can only guess at what's wrong. Also, posting the code that you did was very helpful, keep it up! You can never post too much code! ...Well, within reason... ;P RageInTheMachine9532

          I Offline
          I Offline
          ibok23
          wrote on last edited by
          #4

          It is still saying that the additionalguest is not declared. This is what I wrote. This is all under the button to calculate. Private additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) [I have an error on the last parenthesis - It says end of statement expected.] if nudguest.text > 2 then additionalguestcharge.text = additionalguest [I have an error under additonaguest - it says the additionalguest has not been declared] additionalguestcharge.text = format (additionalguest, "currency")[I have an error under additonaguest - it says the additionalguest has not been declared] Well what am I doing wrong now? lol :eek: Thank you, ibok23

          W D 2 Replies Last reply
          0
          • I ibok23

            It is still saying that the additionalguest is not declared. This is what I wrote. This is all under the button to calculate. Private additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) [I have an error on the last parenthesis - It says end of statement expected.] if nudguest.text > 2 then additionalguestcharge.text = additionalguest [I have an error under additonaguest - it says the additionalguest has not been declared] additionalguestcharge.text = format (additionalguest, "currency")[I have an error under additonaguest - it says the additionalguest has not been declared] Well what am I doing wrong now? lol :eek: Thank you, ibok23

            W Offline
            W Offline
            Wayne Phipps
            wrote on last edited by
            #5

            ibok23 wrote: Private additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) [I have an error on the last parenthesis - It says end of statement expected.] You have to many parenthesis here, count the oppening and closing brackets, there should be one closing bracket ")" for each opening bracket "(". I Think the code should be.. Private additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text) Nadroj wrote: since you have declared additionalguest as a private variable, it can only be accessed in the method it is defined Nadroj is quite write, you cannot access the variable outside the code block in which you declared. From the code snipet you posted, we cannot see if you are doing this. Have you tried declaring it a as class variable? EG Public Class myClass Private additionalguest as double '..other class declarations, methods etc Private Sub CalcCharges additionalguest = ((nudguest.text - 2) * 10) * Val(numberofdays.text) if nudguest.text > 2 then additionalguestcharge.text = additionalguest End Sub Hope this helps Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

            1 Reply Last reply
            0
            • I ibok23

              It is still saying that the additionalguest is not declared. This is what I wrote. This is all under the button to calculate. Private additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) [I have an error on the last parenthesis - It says end of statement expected.] if nudguest.text > 2 then additionalguestcharge.text = additionalguest [I have an error under additonaguest - it says the additionalguest has not been declared] additionalguestcharge.text = format (additionalguest, "currency")[I have an error under additonaguest - it says the additionalguest has not been declared] Well what am I doing wrong now? lol :eek: Thank you, ibok23

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              ibok23 wrote: This is all under the button to calculate. Private additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) [I have an error on the last parenthesis - It says end of statement expected.] OK. Since this is inside the Calculate button CLick handler, you don't need the Private. Also, the error emssage you got is because the parenthesis don't match up. You have 3 open parenthesis and 4 closing. They need to match exactly:

              Dim additionalhuest as Double = ((nudguest.text - 2) \* 10) \* Val(numberofdays.text)
              

              As for the rest, your best bet is to copy the entire Calculate button Click code to a post here. Paste between <pre> and </pre> tags, like this: <pre>Private Sub Calculate_Click(...) Some more code shows up here... and some more... End Sub</pre> When the message is posted, it'll come out looking like this:

              Private Sub Calculate_Click(...)
              Some more code shows up here...
              and some more...
              End Sub

              EDIT: I forgot, don't retype the code you have in your posts. Always Copy and Paste it between a couple of <pre> and </pre> tags or a couple of <code> and </code> tags. RageInTheMachine9532

              I 1 Reply Last reply
              0
              • D Dave Kreskowiak

                ibok23 wrote: This is all under the button to calculate. Private additionalguest as double = ((nudguest.text - 2) * 10) * Val(numberofdays.text)) [I have an error on the last parenthesis - It says end of statement expected.] OK. Since this is inside the Calculate button CLick handler, you don't need the Private. Also, the error emssage you got is because the parenthesis don't match up. You have 3 open parenthesis and 4 closing. They need to match exactly:

                Dim additionalhuest as Double = ((nudguest.text - 2) \* 10) \* Val(numberofdays.text)
                

                As for the rest, your best bet is to copy the entire Calculate button Click code to a post here. Paste between <pre> and </pre> tags, like this: <pre>Private Sub Calculate_Click(...) Some more code shows up here... and some more... End Sub</pre> When the message is posted, it'll come out looking like this:

                Private Sub Calculate_Click(...)
                Some more code shows up here...
                and some more...
                End Sub

                EDIT: I forgot, don't retype the code you have in your posts. Always Copy and Paste it between a couple of <pre> and </pre> tags or a couple of <code> and </code> tags. RageInTheMachine9532

                I Offline
                I Offline
                ibok23
                wrote on last edited by
                #7

                ok, I finally got it to work. This is just a question regarding the number of days (not on the code itself). I can't copy and paste, I am working on two computers at once. My question is if you put on the arrival as Thursday and the checkout as Sunday wouldn't the number of days be 3? Thursday night, friday night and sat. night. When i run the program it says two days. If I need to write my code just let me know, it will not be a problem. Thanks again for giving me all of the suggestions - learning alot. Thank you, ibok23

                D 1 Reply Last reply
                0
                • I ibok23

                  ok, I finally got it to work. This is just a question regarding the number of days (not on the code itself). I can't copy and paste, I am working on two computers at once. My question is if you put on the arrival as Thursday and the checkout as Sunday wouldn't the number of days be 3? Thursday night, friday night and sat. night. When i run the program it says two days. If I need to write my code just let me know, it will not be a problem. Thanks again for giving me all of the suggestions - learning alot. Thank you, ibok23

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  DateDiff is apparently giving you the number of days between the two dates, not inclusive of the actual days that the dates fall on. If you pick Thursday and the very next Friday, DateDiff(Day) will will return 0. If you pick Thursday and Saturday, it will return 1. The solution is to just add 1 to whatever DateDiff returns. RageInTheMachine9532

                  W I 2 Replies Last reply
                  0
                  • D Dave Kreskowiak

                    DateDiff is apparently giving you the number of days between the two dates, not inclusive of the actual days that the dates fall on. If you pick Thursday and the very next Friday, DateDiff(Day) will will return 0. If you pick Thursday and Saturday, it will return 1. The solution is to just add 1 to whatever DateDiff returns. RageInTheMachine9532

                    W Offline
                    W Offline
                    Wayne Phipps
                    wrote on last edited by
                    #9

                    If your using VB.Net there is also the System.Timespan Namespace. You could try something like: Dim d1 As DateTime = DateTime.Now Dim d2 As DateTime = d1.AddMonths(2) Dim ts As System.TimeSpan = d2.Subtract(d1) Dim sDays As String = ts.Days.ToString() Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

                    1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      DateDiff is apparently giving you the number of days between the two dates, not inclusive of the actual days that the dates fall on. If you pick Thursday and the very next Friday, DateDiff(Day) will will return 0. If you pick Thursday and Saturday, it will return 1. The solution is to just add 1 to whatever DateDiff returns. RageInTheMachine9532

                      I Offline
                      I Offline
                      ibok23
                      wrote on last edited by
                      #10

                      Cool, that did work. Thanks again. I maybe calling again on my next project. :-O Thank you, ibok23

                      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