C
C#14mo ago
Gijs

❔ winforms storage system (beginner)

I'm new to winforms and for school I have to make a storage system. I'm completly done the only thing that doesn't work is the selling of shoes, I have no idea how to implement this. I use a datagridview to show the storage, the person selling has a textbox and a button where they can first put the number in the textbox and then click the button to remove x amount of shoes this is my code:
Public mwVoorraad()
{

InitializeComponent();
FillDataGrid();

}
public void FillDataGrid()
{
dt.Rows.Clear();
dt.Columns.Clear();
dt.Columns.Add("Merk");
dt.Columns.Add("Type");
dt.Columns.Add("Maat");
dt.Columns.Add("Kleur");
dt.Columns.Add("Prijs");
dt.Columns.Add("Aantal");

foreach (string[] schoen in voorraad.schoenenvoorraad)
{
dt.Rows.Add(schoen);
}
dgvAdmin.DataSource = dt;
}

private void btVerkoop_Click(object sender, EventArgs e)
{

}

private void btBackToMenu_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Public mwVoorraad()
{

InitializeComponent();
FillDataGrid();

}
public void FillDataGrid()
{
dt.Rows.Clear();
dt.Columns.Clear();
dt.Columns.Add("Merk");
dt.Columns.Add("Type");
dt.Columns.Add("Maat");
dt.Columns.Add("Kleur");
dt.Columns.Add("Prijs");
dt.Columns.Add("Aantal");

foreach (string[] schoen in voorraad.schoenenvoorraad)
{
dt.Rows.Add(schoen);
}
dgvAdmin.DataSource = dt;
}

private void btVerkoop_Click(object sender, EventArgs e)
{

}

private void btBackToMenu_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
if there is anything else you spot wrong with the code please let me know!
88 Replies
Pobiega
Pobiega14mo ago
thats literally just the code that sets up the view you are not showing your vooraad and its declaration, which is where you actually store your data
Gijs
GijsOP14mo ago
This is the vooraad class
internal class voorraad
{
public static List<string[]> schoenenvoorraad = new List<string[]>();

}
internal class voorraad
{
public static List<string[]> schoenenvoorraad = new List<string[]>();

}
and this is the program I hard coded a shoe in here just for testing
static void Main()
{

string[] schoen1 = { "Nike ", "Airmax ", "32 ", "Zwart ", "200 ", "0 "};
voorraad.schoenenvoorraad.Add(schoen1);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new inlogMenu());
}
static void Main()
{

string[] schoen1 = { "Nike ", "Airmax ", "32 ", "Zwart ", "200 ", "0 "};
voorraad.schoenenvoorraad.Add(schoen1);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new inlogMenu());
}
sorry I forgot
Pobiega
Pobiega14mo ago
okay idk dutch, so i don't know if 32 or 200is your "number of shoes in storage" but I see an issue here your data is all strings its hard to do math on strings, as they are... well, not numbers
Gijs
GijsOP14mo ago
it's the 0 at the end
Pobiega
Pobiega14mo ago
how can you sell shoes with 0 in storage? 😛
Gijs
GijsOP14mo ago
I should change that ye xD
Omnissiah
Omnissiah14mo ago
they call that a promise, sometimes a future
Gijs
GijsOP14mo ago
I changed it to 10 for now
Pobiega
Pobiega14mo ago
well, thats honestly not your biggest problem. Your problem is that string is a poor datatype for numbers
Gijs
GijsOP14mo ago
On my other menu the manger can add shoes by adding them I can send that part of the code aswell?
Pobiega
Pobiega14mo ago
that can wait we need to fix your data types do you know any numeric data types, of the top of your head?
Gijs
GijsOP14mo ago
things like int and float?
Pobiega
Pobiega14mo ago
yep!
Gijs
GijsOP14mo ago
oh ye
Pobiega
Pobiega14mo ago
int being whole numbers, and float being numbers with decimals ie 5 vs 7.1234 so which one would be good for storing the amount of shoes?
Gijs
GijsOP14mo ago
so I would use int and not string
Pobiega
Pobiega14mo ago
sounds good! but
string[] schoen1 = { "Nike ", "Airmax ", 32, "Zwart ", 200, 10};
string[] schoen1 = { "Nike ", "Airmax ", 32, "Zwart ", 200, 10};
isnt going to work is it?
Gijs
GijsOP14mo ago
nope you pharse the string to an int right
Pobiega
Pobiega14mo ago
no have you learned about classes yet?
Gijs
GijsOP14mo ago
not really I asked my teacher but he just made one for me without really explaining saying "we teach that later"
Pobiega
Pobiega14mo ago
thats weird you sorta need that to solve this problem nicely the problem we have that a line in your datagrid should represent a single item
Gijs
GijsOP14mo ago
I know you can make them to use them for making "databases" without having a real database or smth like that
Pobiega
Pobiega14mo ago
one ShoeListing or ShoeInventoryRow or something unrelated. lets leave the shoe world for a while
Gijs
GijsOP14mo ago
alright
Pobiega
Pobiega14mo ago
lets say that you want to represent a person, with name and date of birth a single person has ONE name and ONE date of birth. They must always have both. we want these values to be linked somehow can you think of a way to do so right now?
Gijs
GijsOP14mo ago
could you use a list or array for that? or am I just completely miss
Pobiega
Pobiega14mo ago
not really both lists and arrays are for storing multiple things of the same type the real answer here is to make a new type we MAKE a new type that represents out Person
public class Person
{
public string Name { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
}
public class Person
{
public string Name { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
}
Gijs
GijsOP14mo ago
ah I see, so just for my understanding a type is the public class person part?
Pobiega
Pobiega14mo ago
Person is the type
Gijs
GijsOP14mo ago
I see
Pobiega
Pobiega14mo ago
the class is the keyword that declares that this is a class (there are other things that are also types, such as structs, enums, records etc) but for now lets keep it simple and care only about classes
Gijs
GijsOP14mo ago
alright
Pobiega
Pobiega14mo ago
so, lets make a type that represents your shoe-inventory can you try and show me what you come up with?
Gijs
GijsOP14mo ago
public class voorraad
{
public int ShoeListing { get; set; }
pubic ShoeInventoryRow { get; set; }
}
public class voorraad
{
public int ShoeListing { get; set; }
pubic ShoeInventoryRow { get; set; }
}
` would it be something like this? or do I miss understand
Pobiega
Pobiega14mo ago
Oh, I meant each individual row 🙂
Gijs
GijsOP14mo ago
ah
Pobiega
Pobiega14mo ago
Like, make a type that represents { "Nike ", "Airmax ", 32, "Zwart ", 200, 10};
Gijs
GijsOP14mo ago
public class voorraad
{
public string brand { get; set; }
pubic string type { get; set; }
public string size { get; set; }
public string color { get; set; }
public decimal price { get; set; }
public int amount { get; set; }
}
public class voorraad
{
public string brand { get; set; }
pubic string type { get; set; }
public string size { get; set; }
public string color { get; set; }
public decimal price { get; set; }
public int amount { get; set; }
}
like this?
Pobiega
Pobiega14mo ago
Almost! Can you think of a better data type for a price?
Gijs
GijsOP14mo ago
int!
Pobiega
Pobiega14mo ago
Sure! Or maybe even decimal in case we want to be able to say that the price is 199.99
Gijs
GijsOP14mo ago
oh ye true because you work with number behind the .
Pobiega
Pobiega14mo ago
Also, C# naming convention says that classes and properties should be PascalCased so begin with a capital letter
Gijs
GijsOP14mo ago
aha got it so would I make a new class for this or do I add it to my existing code?
Pobiega
Pobiega14mo ago
are those not the same thing? 🙂
Gijs
GijsOP14mo ago
oh they are? 😅
Pobiega
Pobiega14mo ago
sounds like it to me 😄
Gijs
GijsOP14mo ago
aha good to know, so this is added to the first piece of code I sended?
Pobiega
Pobiega14mo ago
Oh, it goes in its own file ideally
Gijs
GijsOP14mo ago
so I creat another one of these? If I understand correctly
No description
Pobiega
Pobiega14mo ago
Yeah thats a code file
Gijs
GijsOP14mo ago
Alright I have done that
Pobiega
Pobiega14mo ago
Cool so, now lets replace that string array we had to keep track of our inventory
Gijs
GijsOP14mo ago
internal class voorraad
{
public static List<string[]> schoenenvoorraad = new List<string[]>();

}
internal class voorraad
{
public static List<string[]> schoenenvoorraad = new List<string[]>();

}
this one right?
Pobiega
Pobiega14mo ago
uh yeah, that too I was thinking about
string[] schoen1 = { "Nike ", "Airmax ", "32 ", "Zwart ", "200 ", "0 "};
voorraad.schoenenvoorraad.Add(schoen1);
string[] schoen1 = { "Nike ", "Airmax ", "32 ", "Zwart ", "200 ", "0 "};
voorraad.schoenenvoorraad.Add(schoen1);
but they both need to be changed
Gijs
GijsOP14mo ago
so I replace them with the public class or?
Pobiega
Pobiega14mo ago
no, we replace the types 🙂 public static List<string[]> schoenenvoorraad is a list of string arrays we want this to be a... ?
Gijs
GijsOP14mo ago
class?
Pobiega
Pobiega14mo ago
no, we already have the class you made it before, ye? this
Gijs
GijsOP14mo ago
yes
No description
Pobiega
Pobiega14mo ago
thats the one fix the class name would you? AantalVooraad if nothing else
Gijs
GijsOP14mo ago
should I just change it to Voorraad
Pobiega
Pobiega14mo ago
Doesnt that mean "Storage"?
Gijs
GijsOP14mo ago
Yes
Pobiega
Pobiega14mo ago
I think thats a bad name to be honest How about ShoeListing, or ShoeInventoryRow? you can translate as needed
Gijs
GijsOP14mo ago
Alright that does sound abit more clear indeed
Pobiega
Pobiega14mo ago
we want our names to accurately represent their content
Gijs
GijsOP14mo ago
I have changed it to ShoeListing
Pobiega
Pobiega14mo ago
ok that is the name of your type our datagrid is a list of shoe listings what is a type that would accurately represent that?
Gijs
GijsOP14mo ago
string?
Pobiega
Pobiega14mo ago
really? I think you already know the type for a list, no?
Gijs
GijsOP14mo ago
I have not heared of that I have only heared of just list
Pobiega
Pobiega14mo ago
Look here
Gijs
GijsOP14mo ago
class?
Pobiega
Pobiega14mo ago
What do you think List<string[]> is?
Gijs
GijsOP14mo ago
ph oh array
Pobiega
Pobiega14mo ago
no its a... ?
Gijs
GijsOP14mo ago
list array?
Pobiega
Pobiega14mo ago
its a list of...? insert the last words 😛
Gijs
GijsOP14mo ago
I'm sorry I'm not really concentrated my normal lesson just started 😅
Pobiega
Pobiega14mo ago
Pay attention to your class then 🙂 this thread isnt going anywhere
Gijs
GijsOP14mo ago
Alright, I think I can mess around from here myself! Still thanks tho and for having the patience to help me!
Pobiega
Pobiega14mo ago
I'm still here if you need help. And there are a few steps we skipped that I think you need to do
Gijs
GijsOP14mo ago
Alright I'll mess around when my lesson is done thank you so much for the help
Pobiega
Pobiega14mo ago
np The answer I was looking for was A list of string arrays btw
Gijs
GijsOP14mo ago
ah thanks
Pobiega
Pobiega14mo ago
List<T> where T = string[]
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server