简体   繁体   中英

How to multiply a value in a textbox by other values in an array (C#)

My task is to display a total when the user enters a quantity of their choice and multiply it by the price on an individual item.

The individual item price is held in an array of 5 and i am unsure how to multiply the price by a number entered inside a textbox?

Info : Using Visual Studio, Windows Forms App .Net Framework

I need to display the total price of a car and multiple that by whatever quantity i enter into a different textbox. Each vehicle has its own description,promo code and price within the array. So what i am wondering is do i need to use the price set in the array for my calculation? or is there a much simpler way of doing it?

My attempt was using a simple if statement then multiplying the textbox by the prices set in the array.

Convert.To Double(textBox9.Text);    
textBox17.Text = textBox9.Text * prices;

Please don't do string math. You need to develop a model ( a class ) that does the math for you, and your UI only displays the data.

public class CartItem
{
    public decimal Price {get; set; }
    public int Quantity {get; set; }
    public decimal Cost { get { return Quantity*Price; } }
}

and on your UI you can do things like

CartItem item;
// .. fill data
if(int.TryParse(textBox9.Text, out int x))
{
    item.Quantity = x;
}

textBox21.Text = item.Cost.ToString("c");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM