简体   繁体   中英

C# Visual Basic Inheritance

I'm just getting started with C# and I've been stuck on this problem for two weeks. I have a main form that gets values from a class and a subclass. My problem is that when I try to create an object of the CorporateClass VB tells me that two of my variables (CarSizeInteger and DiscountInteger) are unassigned. My question is why. I implemented them earlier in the program. Help! I'm hopelessly stuck!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace EX02_CarRentals
{
    public partial class RentalForm : Form
    {
        public RentalForm()
        {
            InitializeComponent();
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void CalculateButton_Click(object sender, EventArgs e)
        {
            int DaysInteger, BeginningOdometerInteger, EndingOdometerInteger, CarSizeInteger, DiscountInteger;

            if (LicenseTextBox.Text != "")

                if (CompactRadioButton.Checked || MidSizeRadioButton.Checked || LuxuryRadioButton.Checked)
                {
                    int.TryParse(DaysRentedTextBox.Text, out DaysInteger);

                    int.TryParse(BeginningOdometerTextBox.Text, out BeginningOdometerInteger);

                    if (BeginningOdometerInteger > 0)
                    {
                        int.TryParse(EndingOdometerTextBox.Text, out EndingOdometerInteger);
                        if (EndingOdometerInteger > 0)
                        {
                            if (CompactRadioButton.Checked)
                                CarSizeInteger = (int)CarSize.Compact;

                            else if (MidSizeRadioButton.Checked)
                                CarSizeInteger = (int)CarSize.MidSize;

                            else CarSizeInteger = (int)CarSize.Luxury;
                        }
                        {
                            if (CorporateRadioButton.Checked || InsuranceRadioButton.Checked)
                            {
                                if (CorporateRadioButton.Checked)
                                    DiscountInteger = (int)Discount.Corporate;

                                else if (InsuranceRadioButton.Checked)
                                    DiscountInteger = (int)Discount.Insurance;

                                //create an instance of the Corporate Class
                                CorporateClass aCorpRental = new CorporateClass(BeginningOdometerInteger, EndingOdometerInteger, CarSizeInteger, DaysInteger, DiscountInteger);
                                AmountDueTextBox.Text = (aCorpRental.getAmountDue()).ToString("C");
                            }
                            else
                            {
                                //create an instance of the Rental Class

                                RentalRate ARental = new RentalRate(BeginningOdometerInteger, EndingOdometerInteger, CarSizeInteger, DaysInteger);
                                AmountDueTextBox.Text = (ARental.getAmountDue()).ToString("C");
                            }
                        }
                    }
                }
            }
        }

        private void DaysRentedTextBox_TextChanged(object sender, EventArgs e)
        {

        }

    }
}

In C#, you cannot use the value of a variable before it's given a value, or you'll get the unassigned variable error. Let's see why your code is giving you that.

I'll focus on CarSizeInteger . There are three places where you attempt to initialize CarSizeInteger :

if (EndingOdometerInteger > 0)
{
    if (CompactRadioButton.Checked)
        CarSizeInteger = (int)CarSize.Compact;
    else if (MidSizeRadioButton.Checked)
        CarSizeInteger = (int)CarSize.MidSize;
    else CarSizeInteger = (int)CarSize.Luxury;
}

Note that all three of these assignment are contained within the if (EndingOdometerInteger > 0) block. In the case that EndingOdometerInteger > 0 is false, none of the assignments will happen. Thus, when you try to use CarSizeInteger later, the compiler detects that it's possible for CarSizeInteger to be uninitialized, which is an error.

To avoid this error, you can give CarSizeInteger a default value:

int CarSizeInteger = 0;

or add an else block to your if statement which initializes CarSizeInteger :

if (EndingOdometerInteger > 0)
{
    // ...
}
else
{
    CarSizeInteger = 0;
}

In any case, just make sure each of your variables is given a value before you tried to read its value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM