-1

CS1503 Argument 1: cannot convert from 'string' to 'int'

Here is the Error that im getting.

 private void btnCalculate_Click(object sender, EventArgs e)
    {
        string strMailingLabel;

        try
        {
            //Create an instance of clsCustomer using the overloaded constructor
            clsCustomer cobjCustomer = new clsCustomer(txtName.Text, txtStreet.Text,
                                txtCity.Text, txtState.Text, txtZip.Text);


            strMailingLabel = cobjCustomer.Name + "\n" +
                              cobjCustomer.Street + "\n" +
                              cobjCustomer.City + ", " +
                              cobjCustomer.State + "  " + cobjCustomer.Zip;

            //Display mailing address
            lblMailingLabel.Text = strMailingLabel;

            //Create an instance of clsOrder using the overloaded constructor
            clsOrder cobjOrder = new clsOrder
                (txtDescription.Text,                 //Error is Here
                 int.Parse(txtQuantity.Text),
                 decimal.Parse(txtPrice.Text));


            cobjOrder.calcExtendedPrice();


            cobjOrder.accumulateTotals();


            lblExtension.Text = cobjOrder.ExtendedPrice.ToString("C");


            lblTotalCount.Text = clsOrder.TotalCount.ToString("N0");
            lblTotalPrice.Text = clsOrder.TotalPrice.ToString("C");
        }

Here is the Order Code

public clsOrder()
    {

    }
    public clsOrder(int intQuantity, decimal decPrice, decimal decDescription)
    {

        this.Quantity = intQuantity;
        this.Price = decPrice;
        this.Description = decDescription;
    }


//declare property methods

    public int Quantity
    {
        get
        {
            return cintQuantity;
        }
        set
        {
            cintQuantity = value;
        }
    }

    public decimal Price
    {
        get
        {
            return cdecPrice;
        }
        set
        {
            cdecPrice = value;
        }
    }

    public decimal Description
    {
        get
        {
            return cdecDescription;
        }
        set
        {
            cdecDescription = value;
        }
    }

I set the description as a decimal, i know that is what i did wrong, the problem is i don't know how to code it correctly. Anyone have an idea?

Jake
  • 11
  • 1
  • 6
  • 1
    Because your `clsOrder` constructor with 3 parameter takes `int` as a first parameter, not `string`. If your text is valid integer, you can parse it to `int` like `new clsOrder(int.Parse(txtDescription.Text)...` Next time, please re-read your code and your error message. Understand your error message. Read it a few times. If you don't understand it, google it. You can find your answer much more faster with this way. You should read http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ from Eric Lippert. – Soner Gönül Oct 13 '15 at 06:47
  • Possible duplicate of [How can I convert String to Int?](http://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int) – Sayse Oct 13 '15 at 06:53
  • The above duplicate was second search result for "cannot convert from 'string' to 'int' c#" – Sayse Oct 13 '15 at 06:53
  • Hmm all the answers given either lead to further errors or just don't work. The Calculate void is completely correct, its something wrong with the clsOrder Code. – Jake Oct 13 '15 at 07:08

2 Answers2

1

You have put wrong order. Try this

clsOrder cobjOrder = new clsOrder(
  Convert.ToInt32(txtQuantity.Text),
  Convert.ToDecimal(txtPrice.Text),
  Convert.ToDecimal(txtDescription.Text));
Harshit
  • 5,147
  • 9
  • 46
  • 93
  • 3
    `TryParse()` should be used instead for `Parse` – sujith karivelil Oct 13 '15 at 06:47
  • The Caclulate void portion of the code is correct, it has something to do with the clsOrder portion of the code, i didn't code the description portion correctly. I'm working on a solution. – Jake Oct 13 '15 at 07:13
0
        //Create an instance of clsOrder using the overloaded constructor
        clsOrder cobjOrder = new clsOrder
            (txtDescription.Text,                 //Error is Here
             int.Parse(txtQuantity.Text),
             decimal.Parse(txtPrice.Text));

It doesn't seem right when looking at the constructor:

public clsOrder(int intQuantity, decimal decPrice, decimal decDescription)
{

    this.Quantity = intQuantity;
    this.Price = decPrice;
    this.Description = decDescription;
}

It requires int, decimal, decimal, your input is string, int, decimal

It starts with an int and then it needs 2 decimals. So it seems to me that you accidentally entered wrong inputs.

What you need is:

        //Create an instance of clsOrder using the overloaded constructor
        clsOrder cobjOrder = new clsOrder
            (int.Parse(txtQuantity.Text),                 //Error is Here
             decimal.Parse(txtPrice.Text)),
             txtDescription.Text);


public clsOrder(int intQuantity, decimal decPrice, string decDescription)
{

    this.Quantity = intQuantity;
    this.Price = decPrice;
    this.Description = decDescription;
}

Also make sure you change the Description to string!

Edit: As some people suggest, you might want to use TryParse. There is plenty to find about how to use this. That's not your main question, so I won't spoonfed you with that.

kevintjuh93
  • 1,010
  • 7
  • 22