1

I've been tasked with a project where I have to use to create forms that digest a list of objects from a file, and is then able to pass the list to another window.

public class Food
{
    public string Name;
    public string Category;
    public int Price;
}

public class Ingredient
{
    public string Name;
    public string Category;
    public decimal PricePerUnit;
    public decimal Quantity;

    public Ingredient(string pName, string pCategory, decimal pPricePerUnit, decimal pQuantity)
    {
        Name = pName;
        Category = pCategory;
        PricePerUnit = pPricePerUnit;
        Quantity = pQuantity;
    }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<Ingredient> Inventory = CallInventoryFile();
    }



    private void inventoryButton_Click(object sender, RoutedEventArgs e)
    {
        InventoryWindow wnd = new InventoryWindow();
        wnd.ShowDialog();
    }

    public List<Ingredient> CallInventoryFile()
    {

        List<Ingredient> ProcessedInventory = new List<Ingredient>();

        try
        {
            string[] fileLines = File.ReadAllLines("Inventory.txt");


            //Reading in the file
            for (int i = 0; i < fileLines.Length; i++)
            {
                string[] CurrentLine = fileLines[i].Split(',');
                string Name = CurrentLine[0].Trim();
                string Category = CurrentLine[1].Trim();
                decimal PricePerUnit = decimal.Parse(CurrentLine[2].Trim());
                decimal Quantity = decimal.Parse(CurrentLine[3].Trim());
                Ingredient IngredientToAdd = new Ingredient(Name, Category, PricePerUnit, Quantity);
                ProcessedInventory.Add(IngredientToAdd);
            }
            return ProcessedInventory;
        }
        catch
        {
            //if we get here read in failed
            MessageBox.Show("There was an error reading in the file");
            return ProcessedInventory;
        }
    }

Which I then have to move onto this window

    public InventoryWindow()
    {
        InitializeComponent();

        categoryComboBox.Items.Add("All");
        categoryComboBox.Items.Add("Pizza");
        categoryComboBox.Items.Add("Burger");
        categoryComboBox.Items.Add("Sundry");
        categoryComboBox.SelectedValue = "All";
    }

    private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }
}

}

My question is how can i pass the results of Inventory from MainWindow to InventoryWindow.

1 Answer 1

5

You can just pass inside the constructor ,

  InventoryWindow wnd = new InventoryWindow(Inventory);
  wnd.ShowDialog();

Then,

public InventoryWindow(List<Ingredient> inputList)
{
}
Sign up to request clarification or add additional context in comments.

1 Comment

That is probably the best way to go. I would personnaly create a public property in the second window public List<Ingredient> Ingredients {get;set;} and then make the call like this InventoryWindow wnd = new InventoryWindow(){ Ingredients = inputList } but that is just a personnal opinion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.