Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3281582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:49:53+00:00 2026-05-17T19:49:53+00:00

I get the error. The way I am looking at is I want the

  • 0

I get the error. The way I am looking at is I want the value that is returned from dayrental() * 19.95 to be stored in rental.

Use of unassigned local variable ‘rental’

 private void button1_Click(object sender, EventArgs e)
    {

        double rental;
        if (checkBox1.Checked == true)
            rental = dayrental() * 19.95;
        label4.Text = Convert.ToString(rental);




    }

    private void label4_Click(object sender, EventArgs e)
    {

    }
    public double dayrental()
    {
        var timeSpan = dateTimePicker2.Value - dateTimePicker1.Value;
        var rentalDays = timeSpan.Days;
        return (double) rentalDays;          
        //label4.Text = Convert.ToString(rentalDays);   
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:49:54+00:00Added an answer on May 17, 2026 at 7:49 pm

    The reason this message appears is that the compiler does not allow you to use an unassigned variable.

    Let’s here consider that your CheckBox.Checked property is false, what happens? You’re trying to convert the value of your variable. At the end, you’re using it! But how should the compiler know what value it’s worth?

    Here’s a link that explains this error in depth: Compiler Error CS0165.

    You need to tel the compiler that you know what you’re doing. How? By initializing a default value to your variable. It may be some of the following:

    double rental = default(double);
    

    or

    double rental = 0;
    

    Or else, you may want that your form displays no value at all when this CheckBox is not Checked. Then, you would need a nullable type of double.

    double? rental = null;
    

    or

    double? rental = default(double?);
    

    While using this nullable double value type, this will allow you to display nothing when the CheckBox.Checked = false. Now, you should ask yourself whether you want zero (0) to be displayed when the CheckBox is not Checked.

    By the way, the if condition only requires a boolean expression in its parentheses, and that’s exactly what the CheckBox.Checked property is, so no need to compare it against true.

    double? rental = null;
    
    if (CheckBox1.Checked) 
        rental = DayRental() * 19.95;
    
    label4.Text = Convert.ToString(rental);
    

    Following this simple rule, you should be able to never ever again get this error.

    Does this help?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to get error messages from a WF4 workflow back
I'm looking for a way to use jQuery with a script index.php that is
I am looking for a way to get certain info from HTML in linux
What would be the best way to get email notifications from error_log errors? For
I use the following jquery statements but i get error in this function onGetDataSuccess(result)
Basically, I want to use the nice Dapper syntax for a stored procedure, without
I have an ASCX that contains <my:Foo ID=Bar runat=server Value='' /> I want to
Few days ago i was looking for way to use my custom php on
I get error: ORA-06575: Package or function GET_CONC_NAMES is in an invalid state When
Why do I get error in this code?Even if i do not link but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.