Ryan Cox
Ryan Cox

Reputation: 51

Trying to write a simple calculator... beginner with C#, can't get past an exception

Okay, so I am modeling this idea off of one of my school projects I had fairly recently and I am not sure why it isn't working at the moment.

My current plan for the program is to use a DataManager to hold a string array containing the numbers a user would input from the menu, and then convert them into integers, and ultimately delineate through them by operators and such. I haven't figured out the last part, and I don't think that idea is going to work like I made it sound. But right now I am having trouble getting the program to store string values in the array.

When I run the program my current goal is to click '1' and press 'Enter', when that happens I want the output to look like 'Number: 1' but I keep throwing an exception.

"An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: At least one element in the source array could not be cast down to the destination array type."

Here is the code from my DataManager class....

public class DataManager 
{ 
public ArrayList calculation;

    public DataManager()
    {
        calculation = new ArrayList();
    }

    public void addNumbers(string n)
    {
        calculation.Add(n);
    }
    // public NumbersAndOperators[] getNumbers()
    //{
      // return  //(NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));
    //}
    public void removeNumbers(NumbersAndOperators n)
    {
        calculation.Remove(n);
    }
    public void addOperators(string o)
    {
        calculation.Add(o);
    }
    public NumbersAndOperators[] getOperators()
    {
        return (NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));
    }
    public void removeOperators(NumbersAndOperators o)
    {
        calculation.Remove(o);
    }

}

}

Here is the code from my NumbersAndOperators class

public class NumbersAndOperators 
{ 
private string number; private string operate;

    public NumbersAndOperators(string n, string o)
    {
        number = n;
        operate = o;
    }
    public void setNumber(string n)
    {
        number = n;
    }
    public void setOperate(string o)
    {
        operate = o;
    }
    public string getNumber(string n)
    {
        return n;
    }
    public string getOperate(string o)
    {
        return o;
    }

}

}

Here is the code from my form

public partial class Form1 : Form 

{ 
   public DataManager data;

    public Form1(DataManager d)
    {
        InitializeComponent();
        data = d;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private void button10_Click(object sender, EventArgs e)
    {
        string n = "0";
        data.addNumbers(n);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        string n = "1";
        data.addNumbers(n);

    }

    private void button2_Click(object sender, EventArgs e)
    {
        string n = "2";
        data.addNumbers(n);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        string n = "3";
        data.addNumbers(n);
    }
    private void button4_Click(object sender, EventArgs e)
    {
        string n = "4";
        data.addNumbers(n);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        string n = "5";
        data.addNumbers(n);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        string n = "6";
        data.addNumbers(n);

    }

    private void button7_Click(object sender, EventArgs e)
    {
        string n = "7";
        data.addNumbers(n);
    }

    private void button8_Click(object sender, EventArgs e)
    {
        string n = "8";
        data.addNumbers(n);
    }

    private void button9_Click(object sender, EventArgs e)
    {
        string n = "9";
        data.addNumbers(n);
    }

    private void division_Click(object sender, EventArgs e)
    {
        string o = "/";
        data.addOperators(o);
    }

    private void multiplication_Click(object sender, EventArgs e)
    {
        string o = "*";
        data.addOperators(o);
    }

    private void subtraction_Click(object sender, EventArgs e)
    {
        string o = "-";
        data.addOperators(o);
    }

    private void addition_Click(object sender, EventArgs e)
    {
        string o = "+";
        data.addOperators(o);
    }

    private void dec_Click(object sender, EventArgs e)
    {
        string n = ".";
        data.addNumbers(n);
    }


    private void equals_Click(object sender, EventArgs e)
    {
        panel1.Controls.Clear();
        NumbersAndOperators[] num = data.getNumbers();
        Label tmp;
        for (int i = 0; i < num.Length; i++)
        {
            tmp = new Label();
            tmp.Text = "Number:" + data.getNumbers();
            tmp.AutoSize = true;
            tmp.Location = new Point(0, 85);

            panel1.Controls.Add(tmp);

        }

    }

}

}

I appreciate any and all advice, this is a problem I haven't seen before which makes it even harder to understand from just reading. I've been googling like a fool for awhile now and everything that comes up I simply do not understand.

Also, I Commented out the line that is throwing the exception

Upvotes: 0

Views: 981

Answers (3)

mrvux
mrvux

Reputation: 8963

When you call:

public void addNumbers(string n)
{
    calculation.Add(n);
}

you add a string to your ArrayList.

When you call:

(NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));

It tries to retrieve data as NumbersAndOperators, which is invalid since you added string to your ArrayList.

Best would be to have your calculation member as:

public List<NumbersAndOperators> calculation;

so you have a type safe list.

Upvotes: 2

Thomas Levesque
Thomas Levesque

Reputation: 292555

On this line:

return  (NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));

You're trying to get an array of NumbersAndOperators, but your ArrayList also contains strings, and strings can't be cast to NumbersAndOperators...

Upvotes: 1

VVS
VVS

Reputation: 19612

I guess you meant to write:

for (int i = 0; i < num.Length; i++)
{
    tmp = new Label();
    tmp.Text = "Number:" + num[i]; // <--
    tmp.AutoSize = true;
    tmp.Location = new Point(0, 85);

    panel1.Controls.Add(tmp);

}

Upvotes: 0

Related Questions