calculation total and display

chageman

Member
Joined
Oct 5, 2019
Messages
9
Programming Experience
Beginner
Hi all, I am trying to calculate the total car rental charge here and then have the amount display when the total charge button is clicked on my form. It keeps showing up as 0. I have been troubleshooting with a friend as well and we cant figure it out. Anyone able to help?

here is my class code:

C#:
public class CarRental
{
    #region "Properties/Fields"

        //5 Instance Properties

        //auto implemented publice get, set
        public string CustomerName { get; set; }

    //auto implemented public get, private set
    public decimal RentalCharge { get; private set; }

    // three private fields

    private int beginOdmometerReading;
    private int daysRented;
    private int endOdometerReading;

    public int BeginOdometerReading
    {
        get
        {
            return beginOdmometerReading;
        }
        set
        {
            beginOdmometerReading = value;
        }
    }

    public int EndOdometerReading
    {
        get
        {
            return endOdometerReading;
        }
        set
        {
            endOdometerReading = value;
        }
    }

    public int DaysRented
    {
        get
        {
            return daysRented;
        }
        set
        {
            daysRented = value;
        }
    }

    #endregion

        #region "Cunstructors"

        //Overloaded Constructor to instantiate object and set four properties

        public CarRental(string customerName, int beginOdometerReading, int endOdometerReading, int daysRented)
    {
        CustomerName = customerName;
        BeginOdometerReading = beginOdometerReading;
        EndOdometerReading = endOdometerReading;
        DaysRented = daysRented;
        CalculateCost();
    }

    // Default Constructor
    public CarRental()
    {
    }

    #endregion

        #region "Methods"

        //Private Instance Method to Calculate rental charge and set crental charge property
        private void CalculateCost()
    {
        //Constant Variables

        const decimal costPerDay = 29.50m;
        const decimal costPerMile = 0.45m;
        decimal RentalTotal;

        //Calculate Cost
        RentalTotal = ((EndOdometerReading - BeginOdometerReading) * costPerMile) + (costPerDay * daysRented);

        //set the RentalCharge Property Value
        RentalTotal = RentalCharge;
    }

    #endregion
}


Here is the code for my button trying to call the calculation to show when the button is clicked:
C#:
private void btnCharge_Click(object sender, EventArgs e)
{
    // Display Rental Car Charge amount in Label "Charge"
    CarRental aCarRental = new CarRental(txtName.Text, (int)nBeginOdometer.Value, (int)nEndOdometer.Value, (int)nDaysRented.Value);
    lblCharge.Text = aCarRental.RentalCharge.ToString();
}

thank you very much for any input!!!
 
Last edited by a moderator:
You need to debug your code, not just read it. Place a breakpoint on each method in your code and then step through it in the debugger, testing the state at each step to make sure that everything is as you expect. Clearly, the state will be unexpected at some point and that is the issue you need to hone in on. Even if you can't work out the solution yourself, at least you'll be able to provide us with far more relevant information. The debugger provides Autos, Locals, Watch and Immediate windows to evaluate variables and other expressions at each step and there are various other debugging tools besides. You need to use them.
 
Hey. New to the forum, but thought I'd try to weigh in a bit.

First off, the other reply is the best advice. You need to step thru your code and see the value in each property/var at each step.

I'd also recommend paying careful attention to your var names and when you use them. You have local vars and member properties with very similar names (many only differing by case), which can make eyeball debugging tricky.

I think the solution is not difficult, but, in the interest of learning, I agree that it'd be better for you to step thru and find it, rather than have someone just say "change this."

Good luck. If willing, let us know when you fix and how.


Peace,

-Dave

Sent from my LG-LS998 using Tapatalk
 
Look closer at line 92 and the direction of assignment. Ignore the comment on line 91. (As an aside, this is why most programming style guides recommend not commenting trivial or self-evident code. Trivial code should be easily understood and not require explanation. All it does is add noise instead of clarifying the code.)
 
By the way, "constant variables" is a contradiction. Constants are constant and variables can vary. They are, by definition, opposites.
Thank you for your input. The naming conventions are just following how my teacher wants it done. I have debugged the code but there are no errors found. I have gone through the code line by line and with a friend to try to figure out where I went wrong. We tried several things to fix the issue but weren't successful. This was just my last effort to see if I could learn what I did wrong and how to fix the issue so I know going forward. Thank you!
 
Look closer at line 92 and the direction of assignment. Ignore the comment on line 91. (As an aside, this is why most programming style guides recommend not commenting trivial or self-evident code. Trivial code should be easily understood and not require explanation. All it does is add noise instead of clarifying the code.)
 
Thank you! Most the comments etc. are there because my teacher wants it formatted that way. I will take a look. I appreciate it!
 
Hey. New to the forum, but thought I'd try to weigh in a bit.

First off, the other reply is the best advice. You need to step thru your code and see the value in each property/var at each step.

I'd also recommend paying careful attention to your var names and when you use them. You have local vars and member properties with very similar names (many only differing by case), which can make eyeball debugging tricky.

I think the solution is not difficult, but, in the interest of learning, I agree that it'd be better for you to step thru and find it, rather than have someone just say "change this."

Good luck. If willing, let us know when you fix and how.


Peace,

-Dave

Sent from my LG-LS998 using Tapatalk
Thank you for your input. I debugged the code
but there are no errors found. I have gone through the code line by line and with a friend to try to figure out where I went wrong. We tried several things to fix the issue but weren't successful. This was just my last effort to see if I could learn what I did wrong and how to fix the issue so I know going forward. Thank you!
 
Line 89 makes line 92 a little redundant at a glance, does it not?
You have local vars and member properties with very similar names (many only differing by case), which can make eyeball debugging tricky.
There is nothing wrong with how he has wrote it. Actually It falls inline with naming conventions and makes it easier to follow the code our OP has written.

but there are no errors found.
I disagree. I would put a debug point on your line 11, and start looking at creating functions for returning values instead of empty properties.
 
Line 89 makes line 92 a little redundant at a glance, does it not?

There is nothing wrong with how he has wrote it. Actually It falls inline with naming conventions and makes it easier to follow the code our OP has written.
Was only thinking that if he wasn't dealing with class property RentalCharge, and a local variable RentalTotal, perhaps he'd see his error. Typically properties are capitalized, and locals are not. Every place has their own styles, but we typically use _foo for a private member var, Foo for a public property, and foo for a local variable. My assumption was that the similarity of the names is why he's not seeing his mistake.

Anyway, just trying to nudge him towards his error.

Peace,

-Dave
 
I use the leading underscore as well for my private class variables. I remember how annoying it was when StyleCop complains about the leading underscore, and to make matters worse, it would say I should be using this.. It's usually one of the first rules I turn off. :devilish:

As an aside, the original .NET naming convention actually recommends against the underscore prefixes. ("Don't use Hungarian." "Don't use prefixes.") It looks like current the naming guidelines has backed off a little bit and now says that the "no prefix" convention only applies to public and protected, but not private and internal. I'm guessing it's because people pointed out the irony of the code in references.microsoft.com and in the code that Microsoft has open sourced on GitHub.
 
My assumption was that the similarity of the names is why he's not seeing his mistake.
I get what you meant and what you were aiming to do. But I think the construction of the code along with the lack of experience is why they're not seen the mistake, and not because of naming or naming convention rules. The code construction could be massively simplified for better readability.
I use the leading underscore as well for my private class variables.
I try not to do that personally, unless I am in debt to using a number of similarly named classes whom are using the same object names in both classes, and or; where derived class(es) have identical names. (Exceptionally rare circumstance). Although I must admit; when finding yourself in such a situation. One would be best merging both classes into one, to rid such a mess from being created in the first place, which is what I generally do. Or alternatively I'll create an interface and set the rules in which the class has a contract with. ;)
 
Was only thinking that if he wasn't dealing with class property RentalCharge, and a local variable RentalTotal, perhaps he'd see his error. Typically properties are capitalized, and locals are not. Every place has their own styles, but we typically use _foo for a private member var, Foo for a public property, and foo for a local variable. My assumption was that the similarity of the names is why he's not seeing his mistake.

Anyway, just trying to nudge him towards his error.

Peace,

-Dave
Thank you. The banking
 
Thank you all for your input. Also though, I am not a he. I will look at my code more. I'm very new to this so was just trying to find an avenue for help and learning since my teacher has not been good with explaining. Thank you all!
 
Back
Top Bottom