user3236592
user3236592

Reputation: 73

Class is not returning an object

it's not recognizing my lstSectionNumber (Tickets.TicketForm.lastSectionNumber is inaccessible due to its protection level). Hopefully I explained everything clearly, I'm just recently started learning C#, any help would be appreciated. Thank you in advance.

public class TicketOrder
{
    private static int premiumTicket = 40;
    private static int basicTicket = 20;
    private static int serviceCharge = 2;
    private TicketForm form = new TicketForm();

    public double CalcTicketCost(int section, double quantity)
    {
        double amount = 0;
        if (int.Parse(form.lstSectionNumber.SelectedItem.Value) <= 150)
        {
            amount = (quantity*premiumTicket) + (quantity*serviceCharge);
            return amount;
        }
        else
        {
            amount = (quantity*basicTicket) + (quantity*serviceCharge);
        }
        return amount;
    }
}

Code-behind:

public partial class TicketForm : System.Web.UI.Page
{
    int myInt;

    public const double premiumTicket = 40;
    public const double basicTicket = 20;
    public const double serviceCharge = 2;
    TicketUser user = new TicketUser();
    TicketOrder order = new TicketOrder();

    protected void Page_Load(object sender, EventArgs e)
    {
        // This disables the unobtrusive validation in order to see the result

        this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;

            // This will keep my five numbers in my list box without adding more with a refresh of a page
            if (!IsPostBack)
            {

                Random RandomSection = new Random();
                for (int i = 0; i < 5; i++)
                {
                    // Created a list box with five random numbers from 1 to 300
                    myInt = RandomSection.Next(1, 300);
                    lstSectionNumber.Items.Add(myInt.ToString());

                }
            }

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // I also added TextMode="Number"to my <asp:TextBox ID="txtNumberOfTickets" 
        //  runat="server" Height="18px" Width="134px"></asp:TextBox> code in order
        // to get rid of an error incase I left my txtNumberOfTicket empty
        int section = Convert.ToInt32(lstSectionNumber.SelectedValue);

    double ticketQuantity = Convert.ToInt32(txtNumberOfTickets.Text);


    //This label displays users first and last names, number of tickers, seat section number and ticket price
    lblPrice.Text = txtFirstName.Text + " " + 
                    txtLastName.Text + ", " + 
                    txtNumberOfTickets.Text + " " + 
                    "Tickets in Section:" + " " + 
                    lstSectionNumber.SelectedValue + "," + 
                    " Total Cost is: " + 
                    order.CalcTicketCost(section, ticketQuantity).ToString("C");
  }

    protected void btnCreateAccount_Click(object sender, EventArgs e)
    {
        user.firstName = txtFirstName.Text;
        user.lastName = txtLastName.Text;
        user.username = txtUserName.Text;
        lblCreateAccount.Text = user.CreateAccount();
    }
}

Upvotes: 0

Views: 126

Answers (2)

user3236592
user3236592

Reputation: 73

Well, I figured it out.

 if (section <= 150)
   { amount = (quantity * premiumTicket) + (quantity * serviceCharge);
    return amount;

Upvotes: 0

ohmusama
ohmusama

Reputation: 4215

Simply give your constructor an empty body

change

public TicketOrder() 

to

public TicketOrder() { }

TO solve your other problem, you are trying to make a NEW page, but asp.NET has already made a page for this page load and populated the values for you. You should Pass the current page object into your TicketOrder constructor and save instead.

Like so:

public TicketOrder(TicketForm form)
{
    this.form = form;
}

and change in TicketOrder

private TicketForm form = new TicketForm();

to

private TicketForm form;

and change in TicketForm

TicketOrder order = new TicketOrder();

to

TicketOrder order;
public TicketForm()
{
    this.order = new TicketOrder(this);
}

Upvotes: 1

Related Questions