Reservations Form Error- Help C# .net

Getting 3 instances of this error mesage

Page 1 of 1

9 Replies - 1755 Views - Last Post: 24 June 2010 - 06:13 PM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Reservations Form Error- Help C# .net

Posted 23 June 2010 - 08:58 PM

I finally got the code to work, but now I am getting 3 instances of this error message: Expected class, delegate, enum, or struct. and it is going to the bool on public bool ...
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 Project_9_2
{
    public partial class FrmReservations : Form
    {
        public FrmReservations()
        {
            InitializeComponent();
        }

        private void FrmReservations_Load(object sender, EventArgs e)
        {
            DateTime currentDate = DateTime.Today;            
            txtArrivalDate.Text = currentDate.ToString("d");
            txtDepartureDate.Text = currentDate.AddDays(3).ToString("d");
        }
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
               if (IsValidData())
                {

                    DateTime arrivalDate = DateTime.Parse(txtArrivalDate.Text);
                    DateTime departureDate = DateTime.Parse(txtDepartureDate.Text);
                    
                    decimal totalPrice = numNights * 115;

                    if(arrivalDate > currentDate.AddYears(5))
                    {
                        MessageBox.Show("Arrival Date must be less than 5 years");
                    }
                    else
                    {
                    TimeSpan ts = departureDate.Subtract(arrivalDate);
                    int numNights = ts.Days;
                    }
               
                    txtArrivalDate.Text = arrivalDate.ToShortDateString();
                    txtDepartureDate.Text = departureDate.ToShortDateString();
                    txtNumNights.Text = numNights.ToString();
                    txtTotalPrice.Text = totalPrice.ToString("c");
                    txtArrivalDate.Focus();
                }
            }
            
         
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" +
                    ex.GetType().ToString() + "\n" +
                    ex.StackTrace, "Exception");
            }
        }
    }
     
  
       
        public bool IsValidData()
        {
            return
                //Validate that arrival and departure dates are on or after current date.
                IsValidDate(txtArrivalDate, "Arrival Date") &&
                //Validate that departure date is after arrival date
                IsValidDate(txtDepartureDate, "Departure Date");
         }

        public bool IsValidDate(TextBox textBox, string name, currentDate)
        {
            if (textBox.Text < currentDate)
            {
                MessageBox.Show(name + " must be later than today, must enter current date or later.", "Entry Error");
                textBox.Focus();
                return false;
            }
            return true;
        }

      
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Reservations Form Error- Help C# .net

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Reservations Form Error- Help C# .net

Posted 23 June 2010 - 09:42 PM

           catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message + "\n\n" + 
                    ex.GetType().ToString() + "\n" + 
                    ex.StackTrace, "Exception"); 
            } 
        } 
    } 


That last '}' there closes your class statement. Don't think you wanted to do that yet.
Was This Post Helpful? 0
  • +
  • -

#3 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Reservations Form Error- Help C# .net

Posted 23 June 2010 - 09:46 PM

I fixed that now it gives me 3 identifier error instances at the end of say public bool IsWithinRange(TextBox textBox, string name)

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 Project_9_2
{
    public partial class FrmReservations : Form
    {
        public FrmReservations()
        {
            InitializeComponent();
        }

        private void FrmReservations_Load(object sender, EventArgs e)
        {
            DateTime currentDate = DateTime.Today;            
            txtArrivalDate.Text = currentDate.ToString("d");
            txtDepartureDate.Text = currentDate.AddDays(3).ToString("d");
        }
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
               if (IsValidData())
                {
                    DateTime currentDate = DateTime.Today;
                    DateTime arrivalDate = DateTime.Parse(txtArrivalDate.Text);
                    DateTime departureDate = DateTime.Parse(txtDepartureDate.Text);
                    TimeSpan ts = departureDate.Subtract(arrivalDate);
                    int numNights = ts.Days;
                    decimal totalPrice = numNights * 115;
                    
                    txtArrivalDate.Text = arrivalDate.ToShortDateString();
                    txtDepartureDate.Text = departureDate.ToShortDateString();
                    txtNumNights.Text = numNights.ToString();
                    txtTotalPrice.Text = totalPrice.ToString("c");
                    txtArrivalDate.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" +
                    ex.GetType().ToString() + "\n" +
                    ex.StackTrace, "Exception");
            }
        }
    
     
        public bool IsValidData()
        {
            return
                //Validate that arrival and departure dates are on or after current date.
                IsValidDate(txtArrivalDate, "Arrival Date") &&
                IsWithinRange(txtArrivalDate, "Arrival Date")&&

                //Validate that departure date is after arrival date
                IsValidDate(txtDepartureDate, "Departure Date");
         }

        public bool IsValidDate(TextBox textBox, string name, currentDate)
        {
            if (textBox.Text < currentDate)
            {
                MessageBox.Show(name + " must be later than today, must enter current date or later.", "Entry Error");
                textBox.Focus();
                return false;
            }
            return true;
        }

       public bool IsWithinRange(TextBox textBox, string name, arrivalDate, currentDate)
       {
           if (arrivalDate > currentDate.AddYears(5))
            {
    MessageBox.Show(name + " must be within 5 years", "Entry Error");
    textBox.Focus();
    return false;
}
    return true;
}



      
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Reservations Form Error- Help C# .net

Posted 24 June 2010 - 01:28 AM

I tried to fix your code, but there is so many things that need to be changed, so I advise you to rethink the logic and rewrite this form. Use DateTimePickers for choosing your date instead of parsing the text from your ArrivateDate & DepartureDate textboxes.
Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Reservations Form Error- Help C# .net

Posted 24 June 2010 - 01:55 AM

OK, this is just a partially fixed solution, although there are still lines of code that would need rewriting...
If you need any additional information about the code, feel free to ask...


namespace Project_9_2
{
    public partial class FrmReservations : Form
    {
        public FrmReservations()
        {
            InitializeComponent();
        }

        private void FrmReservations_Load(object sender, EventArgs e)
        {
            DateTime currentDate = DateTime.Today;            
            txtArrivalDate.Text = currentDate.ToString("d");
            txtDepartureDate.Text = currentDate.AddDays(3).ToString("d");
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsValidData)
                {
                    DateTime currentDate = DateTime.Today;
                    DateTime arrivalDate = DateTime.Parse(txtArrivalDate.Text);
                    DateTime departureDate = DateTime.Parse(txtDepartureDate.Text);

                    TimeSpan ts = departureDate.Subtract(arrivalDate);
                    int numNights = ts.Days;
                    decimal totalPrice = numNights * 115;

                    txtArrivalDate.Text = arrivalDate.ToShortDateString();
                    txtDepartureDate.Text = departureDate.ToShortDateString();
                    txtNumNights.Text = numNights.ToString();
                    txtTotalPrice.Text = totalPrice.ToString("c");

                    txtArrivalDate.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" +
                    ex.GetType().ToString() + "\n" +
                    ex.StackTrace, "Exception");
            }
        }


        public bool IsValidData
        {
            get
            {
                if (IsDate(txtArrivalDate.Text) && IsDate(txtDepartureDate.Text))
                {
                    return IsValidDate(txtArrivalDate, "Arrival Date", DateTime.Today) &&
                           IsWithinRange(txtArrivalDate, "Arrival Date", DateTime.Parse(txtArrivalDate.Text), DateTime.Now) &&
                           IsValidDate(txtDepartureDate, "Departure Date", DateTime.Today);
                }

                return false;
            }
        }
        
        public bool IsDate(string text)
        {
            DateTime date;
            return DateTime.TryParse(text, out date);
        }

        public bool IsValidDate(TextBox textBox, string dateName, DateTime currentDate)
        {
            if (IsDate(textBox.Text))
            {
                if (DateTime.Parse(textBox.Text) >= currentDate)
                {
                    return true;
                }
                else
                {
                    MessageBox.Show(dateName + " must be later than today, must enter current date or later.", "Entry Error");
                    textBox.Focus();
                }
            }

            return false;
        }

        public bool IsWithinRange(TextBox textBox, string dateName, DateTime arrivalDate, DateTime currentDate)
        {
            if (arrivalDate > currentDate.AddYears(5))
            {
                MessageBox.Show(dateName + " must be within 5 years", "Entry Error");
                textBox.Focus();
                return false;
            }

            return true;
        }
      
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: Reservations Form Error- Help C# .net

Posted 24 June 2010 - 05:52 AM

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 Project_9_2
{
    public partial class FrmReservations : Form
    {
        public FrmReservations()
        {
            InitializeComponent();
        }

        private void FrmReservations_Load(object sender, EventArgs e)
        {
            DateTime currentDate = DateTime.Today;            
            txtArrivalDate.Text = currentDate.ToString("d");
            txtDepartureDate.Text = currentDate.AddDays(3).ToString("d");
        }
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
               if (IsValidData())
                {

                    DateTime arrivalDate = DateTime.Parse(txtArrivalDate.Text);
                    DateTime departureDate = DateTime.Parse(txtDepartureDate.Text);
                    
                    decimal totalPrice = numNights * 115;

                    if(arrivalDate > currentDate.AddYears(5))
                    {
                        MessageBox.Show("Arrival Date must be less than 5 years");
                    }
                    else
                    {
                    TimeSpan ts = departureDate.Subtract(arrivalDate);
                    int numNights = ts.Days;
                    }
               
                    txtArrivalDate.Text = arrivalDate.ToShortDateString();
                    txtDepartureDate.Text = departureDate.ToShortDateString();
                    txtNumNights.Text = numNights.ToString();
                    txtTotalPrice.Text = totalPrice.ToString("c");
                    txtArrivalDate.Focus();
                }
            }
            
         
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" +
                    ex.GetType().ToString() + "\n" +
                    ex.StackTrace, "Exception");
            }
        }
    }
     
  
       
        public bool IsValidData()
        {
            return
                //Validate that arrival and departure dates are on or after current date.
                IsValidDate(txtArrivalDate, "Arrival Date") &&
                //Validate that departure date is after arrival date
                IsValidDate(txtDepartureDate, "Departure Date");
         }

        public bool IsValidDate(TextBox textBox, string name, currentDate)
        {
            if (Convert.ToDateTime(textBox.Text) < currentDate)
            {
                MessageBox.Show(name + " must be later than today, must enter current date or later.", "Entry Error");
                textBox.Focus();
                return false;
            }
            return true;
        }

      
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}



for solve complication use datetimepicker control Instead of textbox
Was This Post Helpful? 0
  • +
  • -

#7 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Reservations Form Error- Help C# .net

Posted 24 June 2010 - 06:27 AM

Thank you for your help. I hate that I am so lost on this, my instructor is virtually non existent. I made some corrections on my code, but still am lost as to where to go from there. We don't have anything in our book about the DateTimePicker,
Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Reservations Form Error- Help C# .net

Posted 24 June 2010 - 06:55 AM

View Posttricket_7, on 24 June 2010 - 04:27 AM, said:

We don't have anything in our book about the DateTimePicker,

MSDN learn to use this :)
Was This Post Helpful? 0
  • +
  • -

#9 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Reservations Form Error- Help C# .net

Posted 24 June 2010 - 12:37 PM

Great! Thank you for the link. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#10 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Reservations Form Error- Help C# .net

Posted 24 June 2010 - 06:13 PM

Thank You everyone for your help, I finally got the program working properly. I don't know what I would have done without you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1