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. changing code to add subs and to call on subs

changing code to add subs and to call on subs

Scheduled Pinned Locked Moved Visual Basic
tutorial
12 Posts 3 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 having to change my code so that I will have some subs and so that I can call on the subs. Here is my code for under the btncalculate button. Could anybody give me suggestions on how to put in subs and to change my code where it will still work.

    Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
    Dim amount As Single
    'Finding out how much they will be payig just for the room - with no extras.
    If radstandard.Checked = True Then
    costofroom.Text = standardrate
    costofroom.Text = Format(standardrate, "currency")

        ElseIf raddeluxe.Checked = True Then
            costofroom.Text = deluxerate
            costofroom.Text = Format(deluxerate, "currency")
        ElseIf radsuite.Checked = True Then
            costofroom.Text = suiterate
            costofroom.Text = Format(suiterate, "currency")
    
        End If
    
        'Figuring out how many days that they want the room.
        numberofdays.Text = DateDiff(DateInterval.Day, dtparrival.Value, dtpcheckout.Value) + 1
    
        'Finding out the cost if they need an additional bed.
        Dim newcharge As Double = rollawaybed \* Val(numberofdays.Text)
    
        If chkbed.Checked = True Then
            rollawaybedcharge.Text = Format(newcharge, "currency")
        Else : rollawaybedcharge.Text = 0
            rollawaybedcharge.Text = Format(0, "currency")
        End If
    
        'Finding out how many are going to be in the room.
        'Finding out how much it is going to cost for more than 2 people in the room.
        Dim additionalguest As Double = ((nudguest.Text - 2) \* 10) \* Val(numberofdays.Text)
    
    
        If nudguest.Text > 2 Then
            additionalguestcharge.Text = additionalguest
            additionalguestcharge.Text = Format(additionalguest, "currency")          '((((nudguest.Text - 2) \* 10) \* Val(numberofdays.Text)), "currency")
        Else
            additionalguestcharge.Text = 0
            additionalguestcharge.Text = Format(0, "Currency")
        End If
    
        'Calculating just how much the room is going to cost with the number of Days staying there.
        subtotal.Text = ((numberofdays.Text) \* (costofroom.Text))
        subtotal.Text = Format(((numberofdays.Text) \* (costofroom.Text)), "currency")
    
        'Calculating the total for the room with everything
        total = CInt(CInt(subtotal.Text) + CInt(additionalguestcharge.Text) + CInt(rollawaybedcharge.Text))
    
    I N D 3 Replies Last reply
    0
    • I ibok23

      I am having to change my code so that I will have some subs and so that I can call on the subs. Here is my code for under the btncalculate button. Could anybody give me suggestions on how to put in subs and to change my code where it will still work.

      Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
      Dim amount As Single
      'Finding out how much they will be payig just for the room - with no extras.
      If radstandard.Checked = True Then
      costofroom.Text = standardrate
      costofroom.Text = Format(standardrate, "currency")

          ElseIf raddeluxe.Checked = True Then
              costofroom.Text = deluxerate
              costofroom.Text = Format(deluxerate, "currency")
          ElseIf radsuite.Checked = True Then
              costofroom.Text = suiterate
              costofroom.Text = Format(suiterate, "currency")
      
          End If
      
          'Figuring out how many days that they want the room.
          numberofdays.Text = DateDiff(DateInterval.Day, dtparrival.Value, dtpcheckout.Value) + 1
      
          'Finding out the cost if they need an additional bed.
          Dim newcharge As Double = rollawaybed \* Val(numberofdays.Text)
      
          If chkbed.Checked = True Then
              rollawaybedcharge.Text = Format(newcharge, "currency")
          Else : rollawaybedcharge.Text = 0
              rollawaybedcharge.Text = Format(0, "currency")
          End If
      
          'Finding out how many are going to be in the room.
          'Finding out how much it is going to cost for more than 2 people in the room.
          Dim additionalguest As Double = ((nudguest.Text - 2) \* 10) \* Val(numberofdays.Text)
      
      
          If nudguest.Text > 2 Then
              additionalguestcharge.Text = additionalguest
              additionalguestcharge.Text = Format(additionalguest, "currency")          '((((nudguest.Text - 2) \* 10) \* Val(numberofdays.Text)), "currency")
          Else
              additionalguestcharge.Text = 0
              additionalguestcharge.Text = Format(0, "Currency")
          End If
      
          'Calculating just how much the room is going to cost with the number of Days staying there.
          subtotal.Text = ((numberofdays.Text) \* (costofroom.Text))
          subtotal.Text = Format(((numberofdays.Text) \* (costofroom.Text)), "currency")
      
          'Calculating the total for the room with everything
          total = CInt(CInt(subtotal.Text) + CInt(additionalguestcharge.Text) + CInt(rollawaybedcharge.Text))
      
      I Offline
      I Offline
      ibok23
      wrote on last edited by
      #2

      I was also told to move my subs around. Not sure what this means. Thank you, ibok23

      1 Reply Last reply
      0
      • I ibok23

        I am having to change my code so that I will have some subs and so that I can call on the subs. Here is my code for under the btncalculate button. Could anybody give me suggestions on how to put in subs and to change my code where it will still work.

        Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
        Dim amount As Single
        'Finding out how much they will be payig just for the room - with no extras.
        If radstandard.Checked = True Then
        costofroom.Text = standardrate
        costofroom.Text = Format(standardrate, "currency")

            ElseIf raddeluxe.Checked = True Then
                costofroom.Text = deluxerate
                costofroom.Text = Format(deluxerate, "currency")
            ElseIf radsuite.Checked = True Then
                costofroom.Text = suiterate
                costofroom.Text = Format(suiterate, "currency")
        
            End If
        
            'Figuring out how many days that they want the room.
            numberofdays.Text = DateDiff(DateInterval.Day, dtparrival.Value, dtpcheckout.Value) + 1
        
            'Finding out the cost if they need an additional bed.
            Dim newcharge As Double = rollawaybed \* Val(numberofdays.Text)
        
            If chkbed.Checked = True Then
                rollawaybedcharge.Text = Format(newcharge, "currency")
            Else : rollawaybedcharge.Text = 0
                rollawaybedcharge.Text = Format(0, "currency")
            End If
        
            'Finding out how many are going to be in the room.
            'Finding out how much it is going to cost for more than 2 people in the room.
            Dim additionalguest As Double = ((nudguest.Text - 2) \* 10) \* Val(numberofdays.Text)
        
        
            If nudguest.Text > 2 Then
                additionalguestcharge.Text = additionalguest
                additionalguestcharge.Text = Format(additionalguest, "currency")          '((((nudguest.Text - 2) \* 10) \* Val(numberofdays.Text)), "currency")
            Else
                additionalguestcharge.Text = 0
                additionalguestcharge.Text = Format(0, "Currency")
            End If
        
            'Calculating just how much the room is going to cost with the number of Days staying there.
            subtotal.Text = ((numberofdays.Text) \* (costofroom.Text))
            subtotal.Text = Format(((numberofdays.Text) \* (costofroom.Text)), "currency")
        
            'Calculating the total for the room with everything
            total = CInt(CInt(subtotal.Text) + CInt(additionalguestcharge.Text) + CInt(rollawaybedcharge.Text))
        
        N Offline
        N Offline
        Nadroj
        wrote on last edited by
        #3

        well i dunno how much help this is going to be... very tired, i cant read all that right now, lol.. but do u simply need to create subs for the fact that some1 wants you to and you dont know how? i can start you off with this one. put this one in its own sub 4 sure. to do it, just go and cut the text from the button click event code and make another sub and paste the code in there, as shown: private sub SetCost() 'Finding out how much they will be payig just for the room - with no extras. If radstandard.Checked = True Then costofroom.Text = standardrate costofroom.Text = Format(standardrate, "currency") ElseIf raddeluxe.Checked = True Then costofroom.Text = deluxerate costofroom.Text = Format(deluxerate, "currency") ElseIf radsuite.Checked = True Then costofroom.Text = suiterate costofroom.Text = Format(suiterate, "currency") End If end sub and to call this from the button click code, simply, in the button click event code type SetCost(). it will then go out and do the code in the SetCost method and return to the button click event where it left off. since this section doesnt deal with any of the variables in the rest of the button click code, it can definetly b put in its own sub. you should b able to separate the rest. just b logical about it, look thru it. ex., the ''Figuring out how many days that they want the room.' comment you have there can b cut and pasted into another new sub, called, say, RoomDays, whatever. just follow the same example as the SetCost sub. dont separate parts in2 its own sub if it has variables that are also required in the button click event. ... well, you can use functions for this rather than subs, but keep it simple for now and use subs. lol... forget that last sentence, it was more 4 other ppl reading this and yelling at me about it. NOTE: i STRONGLY suggest (as all programmers will), since you seem somewhat new, start using camel casting for your variables. this means, when u declare a variable (anything..[to an extent].. when you NAME something), use lower case for all letters, except the 1st letter of every word after the 1st word... ex: costofroom should be camel casted to costOfRoom. MUCH easier for other people to read and help you debug code. prettier for the eyes. nearly a standard. ------------------------ Jordan. III

        I 1 Reply Last reply
        0
        • I ibok23

          I am having to change my code so that I will have some subs and so that I can call on the subs. Here is my code for under the btncalculate button. Could anybody give me suggestions on how to put in subs and to change my code where it will still work.

          Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
          Dim amount As Single
          'Finding out how much they will be payig just for the room - with no extras.
          If radstandard.Checked = True Then
          costofroom.Text = standardrate
          costofroom.Text = Format(standardrate, "currency")

              ElseIf raddeluxe.Checked = True Then
                  costofroom.Text = deluxerate
                  costofroom.Text = Format(deluxerate, "currency")
              ElseIf radsuite.Checked = True Then
                  costofroom.Text = suiterate
                  costofroom.Text = Format(suiterate, "currency")
          
              End If
          
              'Figuring out how many days that they want the room.
              numberofdays.Text = DateDiff(DateInterval.Day, dtparrival.Value, dtpcheckout.Value) + 1
          
              'Finding out the cost if they need an additional bed.
              Dim newcharge As Double = rollawaybed \* Val(numberofdays.Text)
          
              If chkbed.Checked = True Then
                  rollawaybedcharge.Text = Format(newcharge, "currency")
              Else : rollawaybedcharge.Text = 0
                  rollawaybedcharge.Text = Format(0, "currency")
              End If
          
              'Finding out how many are going to be in the room.
              'Finding out how much it is going to cost for more than 2 people in the room.
              Dim additionalguest As Double = ((nudguest.Text - 2) \* 10) \* Val(numberofdays.Text)
          
          
              If nudguest.Text > 2 Then
                  additionalguestcharge.Text = additionalguest
                  additionalguestcharge.Text = Format(additionalguest, "currency")          '((((nudguest.Text - 2) \* 10) \* Val(numberofdays.Text)), "currency")
              Else
                  additionalguestcharge.Text = 0
                  additionalguestcharge.Text = Format(0, "Currency")
              End If
          
              'Calculating just how much the room is going to cost with the number of Days staying there.
              subtotal.Text = ((numberofdays.Text) \* (costofroom.Text))
              subtotal.Text = Format(((numberofdays.Text) \* (costofroom.Text)), "currency")
          
              'Calculating the total for the room with everything
              total = CInt(CInt(subtotal.Text) + CInt(additionalguestcharge.Text) + CInt(rollawaybedcharge.Text))
          
          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          ibok23 wrote:

              If radstandard.Checked = True Then
                  _**costofroom.Text = standardrate**
                  costofroom.Text = Format(standardrate, "currency")_
              ElseIf raddeluxe.Checked = True Then
                  _**costofroom.Text = deluxerate**
                  costofroom.Text = Format(deluxerate, "currency")_
              ElseIf radsuite.Checked = True Then
                  _**costofroom.Text = suiterate**
                  costofroom.Text = Format(suiterate, "currency")_
              End If
          

          The bolded sections of your code are unnecessary. You're assigning a value to a TextBox then immediately replacing that value with a different one. Your code is full of this pattern. Shorten it up by removing the first assignment, like this:

              If radstandard.Checked = True Then
                  costofroom.Text = Format(standardrate, "currency")
              ElseIf raddeluxe.Checked = True Then
                  costofroom.Text = Format(deluxerate, "currency")
              ElseIf radsuite.Checked = True Then
                  costofroom.Text = Format(suiterate, "currency")
              End If
          

          RageInTheMachine9532

          I 1 Reply Last reply
          0
          • N Nadroj

            well i dunno how much help this is going to be... very tired, i cant read all that right now, lol.. but do u simply need to create subs for the fact that some1 wants you to and you dont know how? i can start you off with this one. put this one in its own sub 4 sure. to do it, just go and cut the text from the button click event code and make another sub and paste the code in there, as shown: private sub SetCost() 'Finding out how much they will be payig just for the room - with no extras. If radstandard.Checked = True Then costofroom.Text = standardrate costofroom.Text = Format(standardrate, "currency") ElseIf raddeluxe.Checked = True Then costofroom.Text = deluxerate costofroom.Text = Format(deluxerate, "currency") ElseIf radsuite.Checked = True Then costofroom.Text = suiterate costofroom.Text = Format(suiterate, "currency") End If end sub and to call this from the button click code, simply, in the button click event code type SetCost(). it will then go out and do the code in the SetCost method and return to the button click event where it left off. since this section doesnt deal with any of the variables in the rest of the button click code, it can definetly b put in its own sub. you should b able to separate the rest. just b logical about it, look thru it. ex., the ''Figuring out how many days that they want the room.' comment you have there can b cut and pasted into another new sub, called, say, RoomDays, whatever. just follow the same example as the SetCost sub. dont separate parts in2 its own sub if it has variables that are also required in the button click event. ... well, you can use functions for this rather than subs, but keep it simple for now and use subs. lol... forget that last sentence, it was more 4 other ppl reading this and yelling at me about it. NOTE: i STRONGLY suggest (as all programmers will), since you seem somewhat new, start using camel casting for your variables. this means, when u declare a variable (anything..[to an extent].. when you NAME something), use lower case for all letters, except the 1st letter of every word after the 1st word... ex: costofroom should be camel casted to costOfRoom. MUCH easier for other people to read and help you debug code. prettier for the eyes. nearly a standard. ------------------------ Jordan. III

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

            Ok, I have moved that over underneath the Private sub costofroom_textchanged section. Now underneath the btncalculte I wrote costofroom() and I got errors. Now what am I doing wrong? Did I move it to the wrong section? I do not have private sub setcost () I tried to put that under the private sub costofroom and it gave me an error. if i need to copy it over and paste it here let me know. Thank you, ibok23

            D 1 Reply Last reply
            0
            • D Dave Kreskowiak

              ibok23 wrote:

                  If radstandard.Checked = True Then
                      _**costofroom.Text = standardrate**
                      costofroom.Text = Format(standardrate, "currency")_
                  ElseIf raddeluxe.Checked = True Then
                      _**costofroom.Text = deluxerate**
                      costofroom.Text = Format(deluxerate, "currency")_
                  ElseIf radsuite.Checked = True Then
                      _**costofroom.Text = suiterate**
                      costofroom.Text = Format(suiterate, "currency")_
                  End If
              

              The bolded sections of your code are unnecessary. You're assigning a value to a TextBox then immediately replacing that value with a different one. Your code is full of this pattern. Shorten it up by removing the first assignment, like this:

                  If radstandard.Checked = True Then
                      costofroom.Text = Format(standardrate, "currency")
                  ElseIf raddeluxe.Checked = True Then
                      costofroom.Text = Format(deluxerate, "currency")
                  ElseIf radsuite.Checked = True Then
                      costofroom.Text = Format(suiterate, "currency")
                  End If
              

              RageInTheMachine9532

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

              Thanks, I am suppose to be calling on subs and not have everything underneath the button calculate. I am trying the suggestion up top, but not sure where to copy and paste this section and how to call upon it. Thank you, ibok23

              I 1 Reply Last reply
              0
              • I ibok23

                Ok, I have moved that over underneath the Private sub costofroom_textchanged section. Now underneath the btncalculte I wrote costofroom() and I got errors. Now what am I doing wrong? Did I move it to the wrong section? I do not have private sub setcost () I tried to put that under the private sub costofroom and it gave me an error. if i need to copy it over and paste it here let me know. Thank you, ibok23

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

                I've rewritten some of your code in your first post. It should provide a couple of examples for you to follow. You can rewrite three more sections of your original code into Functions. I'll leave it to you to figure out which sections and how to write the functions:

                Private Sub btncalculate\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
                    'Finding out how much they will be payig just for the room - with no extras.
                    **CalculateBaseCostOfRoom()**
                

                'Figuring out how many days that they want the room.
                numberofdays.Text = CalculateDays(dtparrival.Value, dtpcheckout.Value)
                 
                'Finding out the cost if they need an additional bed.
                Dim newcharge As Double = rollawaybed * Val(numberofdays.Text)
                If chkbed.Checked = True Then
                rollawaybedcharge.Text = Format(newcharge, "currency")
                Else
                rollawaybedcharge.Text = Format(0, "currency")
                End If

                 
                'Finding out how much it is going to cost for more than 2 people in the room.
                additionalguestcharge.Text = Format(additionalguest, "currency")
                 
                'Calculating just how much the room is going to cost with the number of Days staying there.
                subtotal.Text = Format(((numberofdays.Text) * (costofroom.Text)), "currency")
                 
                'Calculating the total for the room with everything
                total = CInt(CInt(subtotal.Text) + CInt(additionalguestcharge.Text) + CInt(rollawaybedcharge.Text))
                txttotal.Text = Format(total, "C")

                End Sub

                **' I know this is the long way to write these, but it's easier to follow for beginners...
                

                'This Sub doesn't take any parameters and doesn't return any value to the caller.
                Private Sub CalculateBaseCostOfRoom()
                If radstandard.Checked = True Then
                costofroom.Text = Format(standardrate, "currency")
                ElseIf raddeluxe.Checked = True Then
                costofroom.Text = Format(deluxerate, "currency")
                ElseIf radsuite.Checked = True Then
                costofroom.Text = Format(suiterate, "currency")
                End If
                End Sub

                'This Function takes two DateTime objects as parameters.  These will come from the Value properties
                'of your DateTimePicker controls, dtpArrival and dtpCheckout.  This Function will return a Long type
                'value representing the number of nights these dates span.
                Private Function CalculateDays(ByVal startDa**
                
                1 Reply Last reply
                0
                • I ibok23

                  Thanks, I am suppose to be calling on subs and not have everything underneath the button calculate. I am trying the suggestion up top, but not sure where to copy and paste this section and how to call upon it. Thank you, ibok23

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

                  I think I figured it out. I created its own private sub then called for the sub. I wasn't suppose to put it under the btn for that box. My fault. Thanks for the suggestions and examples - it helps. Tammy:-O Thank you, ibok23

                  N 1 Reply Last reply
                  0
                  • I ibok23

                    I think I figured it out. I created its own private sub then called for the sub. I wasn't suppose to put it under the btn for that box. My fault. Thanks for the suggestions and examples - it helps. Tammy:-O Thank you, ibok23

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

                    ya.. i think u were getting the errors from afew posts earlier from the sub that you made with the "_textchanged" at the end of it.. since you never made a sub called exactly "costofrooms", but rather "costofrooms_textchanged". the error occured b/c there was no sub existing that you were tryting to call (costofrooms()), right? all working now? ------------------------ Jordan. III

                    I 1 Reply Last reply
                    0
                    • N Nadroj

                      ya.. i think u were getting the errors from afew posts earlier from the sub that you made with the "_textchanged" at the end of it.. since you never made a sub called exactly "costofrooms", but rather "costofrooms_textchanged". the error occured b/c there was no sub existing that you were tryting to call (costofrooms()), right? all working now? ------------------------ Jordan. III

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

                      As far as I know. I am just in the process of commenting on the code itself. :-O Thank you, ibok23

                      N 1 Reply Last reply
                      0
                      • I ibok23

                        As far as I know. I am just in the process of commenting on the code itself. :-O Thank you, ibok23

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

                        alrighty. hope i was of some assistance. and remember... CAMEL CASTING. ------------------------ Jordan. III

                        I 1 Reply Last reply
                        0
                        • N Nadroj

                          alrighty. hope i was of some assistance. and remember... CAMEL CASTING. ------------------------ Jordan. III

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

                          don't worry you were. Thanks. Tammy:-D 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