Results 1 to 6 of 6

Thread: [RESOLVED] Custom function code troubleshooting

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Resolved [RESOLVED] Custom function code troubleshooting

    I have the following code below, basically what I want to do is If that text box text field is equal to MI it checks to see if the radio button for preferred is checked then applies that percentage as a discount to a certain total that's added up in a different area of the program. What I can't seem to do is get it to work properly...It seems to apply to discount regardless of what's in that text box, I remove that statement and it works just great. Do I have my Ifs structured in the wrong order for this to work properly? (FYI, the comment is so I know what is going where, so disregard)

    Code:
        Private Function ApplyPreferredCustomerDiscount(ByVal decAmtSale As Single) As Single
            Dim PreferredRate As Single = 0.05
    
            If txtState.Text = "MI" Then
                If RadPreferred.Checked = True Then
                    PreferredRate = 0.05
                Else
                    PreferredRate = 0
                End If
            End If
    
    
            'Pass lblMerchandiseCost
            Return decAmtSale * PreferredRate
    
        End Function
    Any help would be appreciated!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Custom function code troubleshooting

    Read the code. What will be the value of PreferredRate if the TextBox does not contain "MI"?

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: Custom function code troubleshooting

    Quote Originally Posted by jmcilhinney View Post
    Read the code. What will be the value of PreferredRate if the TextBox does not contain "MI"?
    Specifically, read ALL of the code... don't just start with your IF statement... but start with the very first line of code... where the function starts... read EVERY. SINGLE. LINE.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Custom function code troubleshooting

    Reading code is actually a thing you sort of have to see a couple of times to get what it means. I find when someone's given a really short prompt like "read the code", they mechanically read every keyword and say, "So what?"

    Xandler, what they mean is you should look at each line and, in your native language, explain to yourself what it does. Then, step back and read that aloud and see if it does what you want. I think you should also take it one step further, but let's write it out in English first. Here's what we mean by "read the code":

    Set the preferred rate to 0.05.

    If the "state" text box has exactly the string "MI", then consider the state of the check box "preferred". If the state is "MI" and the check box is checked, set the Preferred Rate to 0.05. If the state is "MI" and the check box is not checked, set the Preferred Rate to 0.

    Return the result of multplying the Preferred Rate times the variable that abbreviates "Digital Equipment Corporation American Mountain Time Super Awesome Leadership Experience", or "decamtsale".
    That still might not help you see the problem, so here's the next part. We will pretend we are a computer and execute the program. We want to choose inputs that go down every possible If branch in the program.

    Case 1: The text box is not "MI". What happens? We don't do anything in the If statement, so we return decAmtSale * 0.05, which is logically applying a 95% discount. (It doesn't matter if the checkbox is checked in this case.)

    Case 2: The text box is "MI", and the check box is checked. If both of these are true, we set PreferredRate to 0.05, the same value it starts with. So we return decAmtSale * 0.05, a 95% discount.

    Case 3: The text box is "MI", and the checkbox is not checked. In this case, we set PreferredRate to 0. Since we return decAmtSale * 0, the result is 0.

    Given that, we can rewrite our description of the code more clearly:
    If the customer's state is Michigan and the preferred rate is used, the customer gets a 95% discount. Everyone else pays nothing.
    That's probably not what you wanted. My guess is the biggest problem is you don't understand how to use Double to represent percentages. Here's some hints:
    • If you don't want to discount, then technically you want the "rate" to be 100%. That is 1.00 when expressed as a Double.
    • If you want to apply an x% discount, you multiply the price by 1 - 0.x. So a 5% discount requires the rate to be 1 - 0.05 = 0.95.
    • If you want to INCREASE the price by x%, you multiply by 1 + 0.x. So a 5% tax requires the rate to be 1 + 0.05 = 1.05.


    Give it another go with that knowledge and see if you don't get better results.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: Custom function code troubleshooting

    Thanks for the replies! I managed to figure out although I didn't really understand the generic "read the code"...I'm totally new to VB.Net so I honestly had no clue why it was wrong, but with a little troubleshooting and trial and error, it got fixed.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: [RESOLVED] Custom function code troubleshooting

    When we say "Read the code" we mean read it like it is a story... in plain language:
    Code:
        Private Function ApplyPreferredCustomerDiscount(ByVal decAmtSale As Single) As Single
            Dim PreferredRate As Single = 0.05
    
            If txtState.Text = "MI" Then
                If RadPreferred.Checked = True Then
                    PreferredRate = 0.05
                Else
                    PreferredRate = 0
                End If
            End If
    
    
            'Pass lblMerchandiseCost
            Return decAmtSale * PreferredRate
    
        End Function
    Code:
    Begin a function called ApplyPreferredCustomDiscount
    Declare a variable called PreferredRate and set it to 5%
    Check the state and if it is Michigan... 
    then also check the RadPreferred flag, and if it too is set, then set the PreferredRate to 5%
    if the RadPreferred flag is not set - but we're still set to Michigan, then set the PreferredRate to 0%
    -- at this point, if it isn't the state of Michigan, the PreferredRate is still defaulted to 5%... back from the beginning...
    Return the discount amount.
    So the only time the preferred rate was EVER getting set to 0 was if the txtState was MI AND the RadPreferred flag was not set... in all other conditions it was still 5%

    I don't think that's what you intended.
    I think PreferredRate should only be set if the state was Michigan and hte flag set....but that wasn't how it was written.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width