changing code to add subs and to call on subs
-
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))
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 typeSetCost()
. 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 tocostOfRoom
. MUCH easier for other people to read and help you debug code. prettier for the eyes. nearly a standard. ------------------------ Jordan. III -
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))
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
-
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 typeSetCost()
. 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 tocostOfRoom
. MUCH easier for other people to read and help you debug code. prettier for the eyes. nearly a standard. ------------------------ Jordan. IIIOk, 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
-
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
-
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
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**
-
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 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
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
-
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
-
As far as I know. I am just in the process of commenting on the code itself. :-O Thank you, ibok23
-
alrighty. hope i was of some assistance. and remember... CAMEL CASTING. ------------------------ Jordan. III