CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2009
    Posts
    4

    [RESOLVED] passing array value via class

    here is the code for my program for an assignment. i created it before but am under time constraints and cant seem to get it to work this time. an error is produced and the piece of code and error message it reffers to will be put in the next post for ease of reading.

    public Form1()
    {
    InitializeComponent();
    }
    Employee[] employee = new Employee[10];
    private void button2_Click(object sender, EventArgs e)
    {

    }

    private void button4_Click(object sender, EventArgs e)
    {
    Application.Exit();
    }

    private void button3_Click(object sender, EventArgs e)
    {
    empIDTxt.Text = "";
    fnTxt.Text = "";
    lnTxt.Text = "";
    janTxt.Text = "";
    febTxt.Text = "";
    marTxt.Text = "";
    aprTxt.Text = "";
    mayTxt.Text = "";
    junTxt.Text = "";
    julTxt.Text = "";
    augTxt.Text = "";
    sepTxt.Text = "";
    octTxt.Text = "";
    novTxt.Text = "";
    decTxt.Text = "";
    }

    private void errorChecking()
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
    double avSal;
    string fName;
    string lName;
    int empID;

    double[] wages = new double[12];
    wages[0] = Convert.ToDouble(janTxt.Text);
    wages[1] = Convert.ToDouble(febTxt.Text);
    wages[2] = Convert.ToDouble(marTxt.Text);
    wages[3] = Convert.ToDouble(aprTxt.Text);
    wages[4] = Convert.ToDouble(mayTxt.Text);
    wages[5] = Convert.ToDouble(junTxt.Text);
    wages[6] = Convert.ToDouble(julTxt.Text);
    wages[7] = Convert.ToDouble(augTxt.Text);
    wages[8] = Convert.ToDouble(sepTxt.Text);
    wages[9] = Convert.ToDouble(octTxt.Text);
    wages[10] = Convert.ToDouble(novTxt.Text);
    wages[11] = Convert.ToDouble(decTxt.Text);

    avSal = averageSalary(wages);

    if (empID < 10)
    {

    }
    else
    {
    Employee[empID] = new Employee(Convert.ToInt32(empIDTxt.Text), fnTxt.Text, lnTxt.Text, avSal);
    empID++;
    }
    }

    private double averageSalary(double[] avsal)
    {
    double average = 0;
    double sum = 0;
    for (int index = 0; index < avsal.Length; index++)
    {
    sum = sum + avsal[index];
    }
    average = sum / avsal.Length;
    return average;
    }

    public class Employee
    {
    private int employeeID;
    private double averageWage;
    private string firstName, lastName;

    public Employee(int employeeID)
    {

    }

    public Employee(int empID, string fName, string lName, double avWage)
    {
    employeeID = empID;
    averageWage = avWage;
    firstName = fName;
    lastName = lName;
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: passing array value via class

    Please use code tags so that your code is more readable.

  3. #3
    Join Date
    Jun 2009
    Posts
    4

    Re: passing array value via class

    The piece of code below is where i am sent for the error "employee is a type but used like a variable"
    {
    Employee[empID] = new Employee(Convert.ToInt32(empIDTxt.Text), fnTxt.Text, lnTxt.Text, avSal);
    empID++;
    }

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: passing array value via class

    Quote Originally Posted by max2040uk View Post
    The piece of code below is where i am sent for the error "employee is a type but used like a variable"
    {
    Employee[empID] = new Employee(Convert.ToInt32(empIDTxt.Text), fnTxt.Text, lnTxt.Text, avSal);
    empID++;
    }
    Well, the message seems pretty clear. You have your casing mixed up. The code should be

    Code:
    employee[empID] = new Employee(...)
    Lowercase 'e' to match your variable name.

  5. #5
    Join Date
    Jun 2009
    Posts
    4

    Re: passing array value via class

    sorry i didnt know i could keep the code format
    Code:
    public Form1()
            {
                InitializeComponent();
            }
            Employee[] employee = new Employee[10];
            private void button2_Click(object sender, EventArgs e)
            {
    
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                empIDTxt.Text = "";
                fnTxt.Text = "";
                lnTxt.Text = "";
                janTxt.Text = "";
                febTxt.Text = "";
                marTxt.Text = "";
                aprTxt.Text = "";
                mayTxt.Text = "";
                junTxt.Text = "";
                julTxt.Text = "";
                augTxt.Text = "";
                sepTxt.Text = "";
                octTxt.Text = "";
                novTxt.Text = "";
                decTxt.Text = "";
            }
    
            private void errorChecking()
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                double avSal;
                string fName;
                string lName;
                int empID;
    
                double[] wages = new double[12];
                wages[0] = Convert.ToDouble(janTxt.Text);
                wages[1] = Convert.ToDouble(febTxt.Text);
                wages[2] = Convert.ToDouble(marTxt.Text);
                wages[3] = Convert.ToDouble(aprTxt.Text);
                wages[4] = Convert.ToDouble(mayTxt.Text);
                wages[5] = Convert.ToDouble(junTxt.Text);
                wages[6] = Convert.ToDouble(julTxt.Text);
                wages[7] = Convert.ToDouble(augTxt.Text);
                wages[8] = Convert.ToDouble(sepTxt.Text);
                wages[9] = Convert.ToDouble(octTxt.Text);
                wages[10] = Convert.ToDouble(novTxt.Text);
                wages[11] = Convert.ToDouble(decTxt.Text);
    
                avSal = averageSalary(wages);
    
                if (empID < 10)
                {
    
                }
                else
                {
                    Employee[empID] = new Employee(Convert.ToInt32(empIDTxt.Text), fnTxt.Text, lnTxt.Text, avSal);
                    empID++;
                }
            }
    
            private double averageSalary(double[] avsal)
            {
                double average = 0;
                double sum = 0;
                for (int index = 0; index < avsal.Length; index++)
                {
                    sum = sum + avsal[index];
                }
                average = sum / avsal.Length;
                return average;
            }
    
            public class Employee
            {
                private int employeeID;
                private double averageWage;
                private string firstName, lastName;
    
                public Employee(int employeeID)
                {
    
                }
                
                public Employee(int empID, string fName, string lName, double avWage)
            {
                    employeeID = empID;
                    averageWage = avWage;
                    firstName = fName;
                    lastName = lName;
            }
    
                public void AverageWage()
            {
    
            }

  6. #6
    Join Date
    Jun 2009
    Posts
    4

    Re: passing array value via class

    Thank you all working now i didnt even notice that and ive been looking at it for hours!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured