C# 在另一个方法中调用方法

C# 在另一个方法中调用方法,c#,.net,C#,.net,当产品售出时,我的预期行为是ProductManagement中的ProductSeld调用inventory.UpdateProduct()并动态更新inventory 我应用了一些断点,发现当调用p.productSeld(p1)时,它实际上从未进入inventory.UpdateProduct(),因此inventory从未更新 为了解决这个问题,我在maininv.UpdateProduct(item id,quantity)中显式调用了这个方法,它似乎可以工作,但是,我希望在产品售出时

当产品售出时,我的预期行为是ProductManagement中的ProductSeld调用
inventory.UpdateProduct()
并动态更新inventory

我应用了一些断点,发现当调用p.productSeld(p1)时,它实际上从未进入inventory.UpdateProduct(),因此inventory从未更新

为了解决这个问题,我在main
inv.UpdateProduct(item id,quantity)
中显式调用了这个方法,它似乎可以工作,但是,我希望在产品售出时调用UpdateProduct

class Program
{
    static void Main(string[] args)
    {
        // Products        
        Product p1 = new Product(1, 20, 1000);

        // Inventory 
        Inventory inv = new Inventory();
        ProductManagement p = new ProductManagement();

        // Add Products to inventory
        inv.AddProduct(p1);

        // Products sold
        p.productSold(p1);

        //inv.UpdateProduct(1,50);

        // View Inventory
        inv.ViewInventory();

        Console.ReadLine();
    }
}


“ProductManagement”中的“库存”与主功能中的库存不同

您可以对ProductManagement进行以下更改: (添加带参数的构造函数)

按如下方式更改您的主选项:

static void Main(string[] args)
{     
    Product p1 = new Product(1, 20, 1000);

    Inventory inv = new Inventory();
    ProductManagement p = new ProductManagement(inv); //THE CHANGE

    inv.AddProduct(p1);

    p.productSold(p1);

    inv.ViewInventory();

    Console.ReadLine();
}
class Inventory
{
    private List<Product> inventory = new List<Product>();

    public Inventory()
    {

    }

    public void AddProduct(Product product)
    {
        inventory.Add(product);
    }

    public void UpdateProduct(int id, int quantity)
    {
        foreach(Product p in inventory)
        {
            if (p._id == id)
            {
                Console.WriteLine("product found");
                p._quantity -= quantity;
            }
            else
            {
                Console.WriteLine("cannot find product");
            }
        }
        /* var product = inventory.Single(x => x._id == id);
                      product._quantity -= quantity; */
    }

    public void ViewInventory()
    {
        foreach(Product p in inventory)
        {
            Console.WriteLine("Item ID : {0} Item Price : {1} Item Quantity : {2}", p._id, p._quantity, p._quantity);
        }
    }
}
 class ProductManagement
{
    public Inventory inventory = new Inventory();

    public void productSold(Product product)
    {
        var quantitySold = 1;
        inventory.UpdateProduct(product._id, quantitySold);
    }
}
public class ProductManagement
{
    public Inventory inventory;

    public ProductManagement(Inventory inv)
    {
        this.inventory = inv;
    }

    public void productSold(Product product)
    {
        var quantitySold = 1;
        inventory.UpdateProduct(product._id, quantitySold);
    }
}
static void Main(string[] args)
{     
    Product p1 = new Product(1, 20, 1000);

    Inventory inv = new Inventory();
    ProductManagement p = new ProductManagement(inv); //THE CHANGE

    inv.AddProduct(p1);

    p.productSold(p1);

    inv.ViewInventory();

    Console.ReadLine();
}