0

For a personal project I want to make a simple Inventory system in C#. There are three inputs, itemName(string), itemQuantity(int), itemLevelAlert(int), and then a button.

When the 'Add' button is clicked, I want the input data to be grouped and stored somewhere. I am not experienced in C# enough to know the best way to do this.

In JavaScript I'd probably use a multi-dimension array:

var inventoryArray = [["Hammer", 25, 5], ["Nails", 100, 20]]

and simply push the input to that array. I'm still learning C# and it goes about arrays a bit differently.

Could I do something similar with a list? I'm probably jumping into deep here considering my inexperience but I feel like I learn better that way.

3
  • yes, List is that what you need. Commented Feb 1, 2017 at 3:14
  • 3
    Best option is here: Created a class with necessary properties, then create a List of objects of that class Commented Feb 1, 2017 at 3:15
  • 1
    @un-lucky brilliant! I'm still not to familar with classes but a quick read up and I get what you're saying. The answer by Joel is sort of what I was heading towards. Commented Feb 1, 2017 at 3:30

3 Answers 3

2

In .Net languages like C#, you typically want to build a type (class) for this kind of thing:

public class Item
{
   public string Name {get;set;}
   public int Quantity {get;set;}
   public int AlertLevel {get;set;}
}

And then you create a generic List object that can hold these items (and only these items!)

var inventory = new List<Item>();

And now, when you click add, the code for the button looks something like this (assuming Winforms and with lots of made-up variable names. Web sites will look different):

//assuming Winforms. Code for a web site will look a bit different:
private void button1_Click(object sender, EventArgs e)
{
    int quantity, alertlevel;
    if (!int.TryParse(txtQuantity.Text, out quantity))
    {
       MessageBox.Show("Enter a valid number for the quantity.");
       return;
    }
    if (!int.TryParse(txtQuantity.Text, out alertlevel))
    {
       MessageBox.Show("Enter a valid number for the alert level.");
       return;
    }

    inventory.Add(new Item() {Name = txtName.Text, Quantity = quantity, AlertLevel = alertlevel});
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes! I'm still getting used to using Classes. This actually helped a lot in figuring out how they come into play. I've pretty much done what you did, created a class with the 3 inputs, and then on button click I've done everything except for the validation, thanks for those as well they will come in handy!
Be aware... unlike javascript, .Net and C# are very particular about data types. You don't want to skip the validation! int's are not strings, strings are not ints. You don't convert as freely between them as you do in javascript. Of course, you can do this other ways... not enabling the button unless there is good data in the textboxes, for example. But don't expect to convert this on the fly later. You do want that data stored in the class using good types.
That's something that I don't have the habit of yet, and what also frustrates me, C# being very particular with it's data types. I'm more so learning how things interact (data types, methods, classes) as it's very different to javascript.
1

You can start with defining your model :

internal class Inventory
    {
        public string ItemName { get; set; }

        public int ItemQuantity { get; set; }

        public int ItemLevelAlert { get; set; }
    }

Then, you should declare list of inventory where you will store your newly created inventory :

var inventoryList = new List<Inventory>();

After that, you need to add your inventory in above inventoryList in button click event :

button.Click += (s, e) =>
            {
                inventoryList.Add(new Inventory()
                {
                    // get value from your form
                    ItemName = "Item1",
                    ItemLevelAlert = 1,
                    ItemQuantity = 5
                });

Comments

0

You can use string and int types doing the following:

            (string, int, int)[] Cats = 
            { 
                ("Tom", 20, 3), ("Fluffy", 30, 4), ("Harry", 40, 5), ("Fur Ball", 40, 6) 
            };
            
            foreach (var cat in Cats)
            {
                Console.WriteLine($"My cats name is {cat.Item1} it is {cat.Item2} years old and weights {cat.Item3} lbs.");
            }

Comments

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.