Hello i am trying to solve the second part of this question
i am getting these errors

Error 2 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Error 1 Use of unassigned local variable 'i'

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 EmployeeExceptionDemo2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Employee[] emp = new Employee[5];
        private void button1_Click(object sender, EventArgs e)
        {
            int id;
            double wage;
            for (int i; i < 5; i++)
            {
                label3.Text = " Employee " + (i + 1) + " ID # ";
                id = Convert.ToInt32(textBox1.Text);
                label3.Text += "Hourly Wage ";
                wage = Convert.ToDouble(textBox2.Text);
                emp[i] = new Employee(id, wage);
            }
            label3.Text = " Display Employee details\n ID Number\t\tHourlyWage";
           
        }
        public class Employee
        {
            int IDNum;
            double hourlyWage;
            public Employee() { }
            public Employee(int id, double hw)
            {
                try
                {
                    IDNum = id;
                    if (hw < 6 || hw > 50)
                        throw new ArgumentException("Invalid Value", "houlyWage");
                    else
                    {
                      //  label3.Text = "Employee Object Created Successfully";
                        hourlyWage = hw;
                    }
                }
                catch (ArgumentException ae)
                {
                    IDNum = 999;
                    hourlyWage = 6.0;
                    (ae.Message);
                }
            }
           
       
        }
    }
}

Recommended Answers

All 21 Replies

>for (int i; i < 5; i++)
i is not initialized to anything, it's the cause of error 2. Might I suggest initializing it to 0?

>(ae.Message);
This doesn't do anything, it's the cause of error 2. Most likely you wanted to print ae.Message or log it somewhere.

can you sow me a way to arrange it?

As Narue said problem is in line number 23 and 56 just change lines as follows

line 23 for (int i=0; i < 5; i++)
line 56 MessageBox.Show(ae.Message);

the assignment required that five employee id and the hourly wag will show up how cant it be done

Can you plz explain in your simple words what this program is suppose to do and also tell us the controls used (labe3, textbox1,textbox2,textbox3 etc) because i cannot open the image you have attached so if you will explain in your own words what you understand of this assignment it will be much more helpful

and please mark your all other threads named exception solved so that it will be clear where the where and what is under discussion currently it is creating confusion please mark all of them solved and discuss you current problem here

Hope now you'll be able to open it now

but i will right it again

5a. Create an Employee class with two fields:IDNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, throw an ArgumentException is the hourlyWage is lees than 6.00 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs

5b. using the employee class created in exercise 5a, write an application that create an array of five employees, Prompt the user for values for each field for each employee. If the user enters improper or invalid data, handle any exception that are thrown by setting the employee ID number to 999 and employee's pay rate to the $6.00 minimum. At the end of the program, display all the entered, and possibly corrected, record. Save the file as EmployeeExcptionDemo2

and i am sorry for the multiple threads.

Try the following code textBox3_KeyPress and textBox1_KeyPress are the function to allow only numeric data in textBox1 and textBox3.....

//label1.Text="ID;
//label2.Text="HourlyWage";
//richTextBox--> to display result
//textBox3---> to enter employee id
//textBox1--->to enter hourly wage of employee

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;
using BusinessLayer;

namespace EmployeeExceptionDemo2
{
    public partial class Form1 : Form
    {
             Employee[] emp = new Employee[5];
        int index1 = 0;
        public Form1()
        {
            InitializeComponent();
        }
       
            private void button1_Click(object sender, EventArgs e)
            {
                
                if (index1 < 5)
                {
                    int id = 0;
                    double wage = 0;
                    
                    try
                    {
                       if (textBox3.Text != "" && textBox1.Text != "")
                        {
                            id = Int32.Parse(textBox3.Text);
                            wage = Double.Parse(textBox1.Text);
                            
                        }
                       
                        emp[index1] = new Employee(id, wage);
                        index1++;
                        if (MessageBox.Show("Do you want to enter More Values", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            textBox3.Text = "";
                            textBox1.Text = "";
                            button1_Click(this, e);
                        }

                    }
                    catch (Exception e3)
                    {
                        richTextBox1.Text = " Display Employee details\n ID Number\t\tHourlyWage" + "\n";
                        for (int i = 0; i < index1; i++)
                        {
                            richTextBox1.Text += "\n" + emp[i].getID() + "\t\t" + emp[i].getWage();
                        }
                    }

                }
                else MessageBox.Show("Exceeding Limit");

            }
            void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
                if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                {
                    e.Handled = true;
                }
             }

            void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
                if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
                {
                    e.Handled = true;
                }
                // only allow one decimal point

                if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
                {
                    e.Handled = true;
                }

            }
            
            public class Employee
            {
                int IDNum;
                double hourlyWage;
                public Employee() { }
                public Employee(int id, double hw)
                {
                    //try
                    //{
                        IDNum = id;
                        hourlyWage = hw;

                        if (hw < 6.0 || hw > 50.0)
                        {
                            IDNum = 999;
                            hourlyWage = 6.0;
                            throw new Exception(new EmployeeException(id, hw).errorMessage);
                           
                        }
                        
                    //}
                    //catch (ArgumentException ae)
                    //{
                        
                    //    MessageBox.Show(ae.Message);
                    //}
                  
                }
                public class EmployeeException
                {
                    //instantiate a string variable called errorMessage here.
                    public string errorMessage;

                    //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                    public EmployeeException(int id, double hw)
                    {
                        throw new Exception("For " + id+ " Hourly Wagr is invalid\n...  Hourly Wage is " + hw.ToString("C")  + errorMessage);
                        //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                    }

                }
                public int getID()
                {
                    return IDNum;
                }
                public Double getWage()
                {
                    return hourlyWage;
                }


            }

    }

it dosent recognize e3

catch (Exception e3)

Where you tried to use e3? It is not getting any problem here with me

i made this window and i am getting these output

i made this window and i am getting these output

I used the same values as you did

This is the view with more values

Try this code

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 EmployeeExceptionDemo2
{
    public partial class Form1 : Form
    {
        
       
                    Employee[] emp = new Employee[5];
                    int index1 = 0;
        public Form1()
        {
            InitializeComponent();
        }
       
            private void button1_Click(object sender, EventArgs e)
            {
                
                if (index1 < 5)
                {
                    int id = 0;
                    double wage = 0;
                    
                   
                       if (textBox3.Text != "" && textBox1.Text != "")
                        {
                            id = Int32.Parse(textBox3.Text);
                            wage = Double.Parse(textBox1.Text);
                            
                        }
                        try
                        {
                          
                       if (wage < 6.0 || wage > 50.0)
                       {
                           id = 999;
                           wage = 6.0;
                           emp[index1] = new Employee(id, wage);
                           throw new Exception(new Employee.EmployeeException(id, wage).errorMessage);

                       }
                       else
                       {
                           emp[index1] = new Employee(id, wage);
                           index1++;
                           if (MessageBox.Show("Do you want to enter More Values", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                           {
                               textBox3.Text = "";
                               textBox1.Text = "";
                               button1_Click(this, e);
                           }

                       }
                    }
                    catch (Exception e3)
                    {
                        richTextBox1.Text = " Display Employee details\n ID Number\t\tHourlyWage" + "\n";
                        for (int i = 0; i < index1; i++)
                        {
                            richTextBox1.Text += "\n  " + emp[i].getID() + "\t\t\t" + emp[i].getWage();
                        }
                        richTextBox1.Text += "\n  " + e3.Message;
                    }

                }
                else MessageBox.Show("Exceeding Limit");

            }
           
            
            public class Employee
            {
                int IDNum;
                double hourlyWage;
                public Employee() { }
                public Employee(int id, double hw)
                {
                    //try
                    //{
                        IDNum = id;
                        hourlyWage = hw;

                        //if (hw < 6.0 || hw > 50.0)
                        //{
                        //    IDNum = 999;
                        //    hourlyWage = 6.0;
                        //    throw new Exception(new EmployeeException(IDNum, hourlyWage).errorMessage);
                           
                        //}
                        
                    //}
                    //catch (ArgumentException ae)
                    //{
                        
                    //    MessageBox.Show(ae.Message);
                    //}
                  
                }
                public class EmployeeException
                {
                    //instantiate a string variable called errorMessage here.
                    public string errorMessage;

                    //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                    public EmployeeException(int id, double hw)
                    {
                        throw new Exception("For " + id+ " Hourly Wagr is invalid\n...  Hourly Wage is " + hw.ToString("C")  + errorMessage);
                        
                        //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                    }

                }
                public int getID()
                {
                    return IDNum;
                }
                public Double getWage()
                {
                    return hourlyWage;
                }


            }
}
}

i need to declare the bottons thats way

no you can only use one button the second button in for some other function not related to your problem I have pasted the code check it and compare it with your previous one

write line number 69 as MessageBox.Show(e3.Message); and close the else part after line number 51

Had to start all over so everything will recognize each other. Thanks you are tremendous help,
(do you know java by any chance ?), you are good guy, and an excellent programmer.
cheers, i will be in touch next week i have 3 more weeks till my semester is over

Thanks :) for your appreciation yes I know Java a little bit not as good as C# but yes I know it and Let me tell you a secret I am a girl dear not a guy :D

Thanks :) for your appreciation yes I know Java a little bit not as good as C# but yes I know it and Let me tell you a secret I am a girl dear not a guy :D

wow you are my idol, i always dreamt to meet a geek like me
is there a way that we can transfer personal details
and for the java question i have all the code i need to finish something that bugs me

Please mark all your solved threads as solved now because there are so many threads named exception which are unsolved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.