❔ Item to number

i need to give each item a price and then make a code to add the prce together and give it as an result, most of that not hard but how i can add number value to item exactly
128 Replies
Thinker
Thinker2y ago
Make each item a custom class containing a name and a price instead of just a string.
Saotom Gosling
Saotom GoslingOP2y ago
ok, may you share the command with me? pls i am asking respectfully i am just a person in need
Thinker
Thinker2y ago
Do you know how to make a class?
Saotom Gosling
Saotom GoslingOP2y ago
im new is it just class.new meh
Thinker
Thinker2y ago
Is this part of an assignment or something?
Saotom Gosling
Saotom GoslingOP2y ago
trying to do something by myself whenever i dont know something i usualy look at my other projects
Thinker
Thinker2y ago
right
Thinker
Thinker2y ago
You could try looking through this introduction to classes. They're a pretty basic building block of C#, so they're very useful to know about. https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/classes
Classes and objects - C# Fundamentals tutorial
Create your first C# program and explore object oriented concepts
Thinker
Thinker2y ago
But essentially, you make a class with two properties; a name and a price. You can then (assuming you're using a List) do List<YourClass> instead of List<string>, and then .Items.Add(new YourClass(name, price))
Saotom Gosling
Saotom GoslingOP2y ago
ok cool well im looking in it and it will tkae time to understand so uhm quick? im using checkboxes so no a combobox i mean any quicker code? i apologize many times if i appear impatient or rude, i just need the code becasue as much as i myself wish, i do not have time to study classes this evening
Thinker
Thinker2y ago
You won't learn (as much) if someone just gives you the code
Saotom Gosling
Saotom GoslingOP2y ago
i know, i will look into it. I learn quicker by looking at something i again apologize for myself
Thinker
Thinker2y ago
But sure, here's a quick example
public class HardwareComponent
{
public string Name { get; }
public decimal Price { get; }

public HardwareComponent(string name, decimal price)
{
Name = name;
Price = price;
}
}
public class HardwareComponent
{
public string Name { get; }
public decimal Price { get; }

public HardwareComponent(string name, decimal price)
{
Name = name;
Price = price;
}
}
List<HardwareComponent> items = new();
items.Add(new HardwareComponent("Gigabyte Z790 Aorus Xtreme", 150m));
items.Add(new HardwareComponent("Gigabyte Z690 Aorus Pro", 120m));
items.Add(new HardwareComponent("MSI MAG B660M WIFI DDR4", 100m));
List<HardwareComponent> items = new();
items.Add(new HardwareComponent("Gigabyte Z790 Aorus Xtreme", 150m));
items.Add(new HardwareComponent("Gigabyte Z690 Aorus Pro", 120m));
items.Add(new HardwareComponent("MSI MAG B660M WIFI DDR4", 100m));
Saotom Gosling
Saotom GoslingOP2y ago
thx very much, i appreciate every bit of effort i will credit u in my project your actions will not be forgotten i am incredibly sorry to disturb your time again, but it doesnt let me/doesnt recognize items after new();
Thinker
Thinker2y ago
it's fine show your code, what errors are you getting?
Saotom Gosling
Saotom GoslingOP2y ago
Angius
Angius2y ago
$prettycode
MODiX
MODiX2y ago
To format your code in Visual Studio, Visual Studio Code, Rider, use the following shortcut:
Visual Studio CTRL + K + D
Rider / Resharper CTRL + ALT + L
Visual Studio Code SHIFT + ALT + F
Visual Studio CTRL + K + D
Rider / Resharper CTRL + ALT + L
Visual Studio Code SHIFT + ALT + F
NOTE: the first key must be held while doing it. https://cdn.discordapp.com/attachments/569261465463160900/899513918567890944/2021-10-18_01-26-35.gif
Angius
Angius2y ago
Judging by the braces you might have some of that code placed where it doesn't belong Proper indentation makes this sort of an issue very easy to spot
Saotom Gosling
Saotom GoslingOP2y ago
well uhm so the } appears to be a problem aswell for a reason
Angius
Angius2y ago
Count your {s and }s And see if their numbers match
Thinker
Thinker2y ago
It looks like you've put the class inside a method, which isn't right.
Saotom Gosling
Saotom GoslingOP2y ago
yeah i forgot on }
Thinker
Thinker2y ago
Also the List<HardwareComponent> items = new(); stuff was just an example
Saotom Gosling
Saotom GoslingOP2y ago
its currently in public partial class frm_Specs : Form ah ohhhhhhh i got it now uhm well maybe i dont
Accord
Accord2y 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.
Saotom Gosling
Saotom GoslingOP2y ago
get set still is damn complicated
Denis
Denis2y ago
Properties - C# Programming Guide
A property in C# is a member that uses accessor methods to read, write, or compute the value of a private field as if it were a public data member.
Preda
Preda2y ago
Shouldn t it be
internal List<HardwareComponent> Items =
new List<HardwareComponent>();
internal List<HardwareComponent> Items =
new List<HardwareComponent>();
?
Angius
Angius2y ago
No I mean, it can be, but why?
Preda
Preda2y ago
I have never used only
= new();
= new();
what is diffrent?
Thinker
Thinker2y ago
List<HardwareComponent> Items = new List<HardwareComponent>(); and List<HardwareComponent> Items = new(); are literally the same except the second is shorter
Preda
Preda2y ago
Hmm
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
List<int> xs = new();
xs.Add(1);
xs
List<int> xs = new();
xs.Add(1);
xs
Result: List<int>
[
1
]
[
1
]
Compile: 514.313ms | Execution: 27.219ms | React with ❌ to remove this embed.
Accord
Accord2y 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.
Saotom Gosling
Saotom GoslingOP2y ago
I GOT AN IDEA FINALLY its just like excel nevermind
Preda
Preda2y ago
@Officer Saotome u still need help?
Saotom Gosling
Saotom GoslingOP2y ago
yep, i really wanna learn how to do it simply i attempted figuring out a new way
Saotom Gosling
Saotom GoslingOP2y ago
my attempt is, the preise (prices) textbox is supposed to show a diffrent price depednign on the picked item, so i try giving it values
Saotom Gosling
Saotom GoslingOP2y ago
i am trying to make things edible
Saotom Gosling
Saotom GoslingOP2y ago
i dont know if my idea is to complicated but what i want ot do makes sense to me
Pobiega
Pobiega2y ago
I'm a bit worried about that event handlers name and the code it runs "when cb_motherboards selected index changes, add 3 motherboards to it"
Saotom Gosling
Saotom GoslingOP2y ago
why that ohh i see but it doesnt make much of a difference for the enduser, i can place that code somewhere else sure
Preda
Preda2y ago
Its a Drop Down List
Pobiega
Pobiega2y ago
it makes a lot of difference every time I select something, you add 3 new duplicate options
Saotom Gosling
Saotom GoslingOP2y ago
so i just copy that items outside of that event in the load for example?
Pobiega
Pobiega2y ago
are they just supposed to be the default items? in that case, I suggest adding them via the designer, or in the constructor.
Saotom Gosling
Saotom GoslingOP2y ago
just the items that are there, but for what i want to do i have to replace them sure i will build it that way i have it for now and change when i planmned on doing bigger stuff
Saotom Gosling
Saotom GoslingOP2y ago
i placed them in the frm load, thats better right?
Pobiega
Pobiega2y ago
ye then its a one time event, instead of happening multiple times
Saotom Gosling
Saotom GoslingOP2y ago
good
Preda
Preda2y ago
You can create a list in the form (when form is strated create a list)
Saotom Gosling
Saotom GoslingOP2y ago
uhm well i dont know how to do a list, i work with what i know right now
Pobiega
Pobiega2y ago
keep it as is for now
Saotom Gosling
Saotom GoslingOP2y ago
even if it becomes a mess, i wanna expand this programm little for little
Pobiega
Pobiega2y ago
Now, if I understand your question correctly, you want to associate prices with these items, yes?
Saotom Gosling
Saotom GoslingOP2y ago
yes, one sec ima try to give a visual example
Pobiega
Pobiega2y ago
each MB having its own price, each cpu having its own price etc right have you learnt about classes and/or records yet?
Saotom Gosling
Saotom GoslingOP2y ago
each combobox has three items, and each combobox has a textbox, the textbox is supposed to display the the selected items worth and in th end i want to calcualte all 3 textboxes togehter to make a final price, of cours not the textbox literla ybut starting liek my example
Pobiega
Pobiega2y ago
I understand
Preda
Preda2y ago
combobox.SelectedValue
Saotom Gosling
Saotom GoslingOP2y ago
and i got plenty ideas from that
Pobiega
Pobiega2y ago
have you learnt about classes and/or records yet?
Saotom Gosling
Saotom GoslingOP2y ago
not yet, i have a few private hours learning C# but im not in the actual school yet, so im trying to do bigger stuff myself it may sound silly but i wanna impress my teacher with the silly stuff i made form ym little knowledge
Pobiega
Pobiega2y ago
Well, what you want to do here is associate a string (the item name) with a number (the item price) The easiest way to do that is to make a record, like so : public record SaotomeItem(string Name, int Price);
Saotom Gosling
Saotom GoslingOP2y ago
did i give it a value yet? yeah i hae heard that before, i tried makign stuff myself up because i idnt understand it yet //code by thinker227#5176 from the C# Discord public class HardwareComponent { public string Name { get; } public decimal Price { get; } public HardwareComponent(string name, decimal price) { Name = name; Price = price; }
Pobiega
Pobiega2y ago
we can then create an instance of that like so: var mb = new SaotomeItem("Pobiega Extreme Motherboard", 12312);
Preda
Preda2y ago
When adding things to a DDL normally it would assign the value
Pobiega
Pobiega2y ago
yep, thats pretty much the same thing
Saotom Gosling
Saotom GoslingOP2y ago
var, well i gotta figure that out what does the letters after var mean cause im trying to undeerstand every input i get
Pobiega
Pobiega2y ago
mb? thats just a variable variable name, I should say
Saotom Gosling
Saotom GoslingOP2y ago
i see oh i think i understand
Pobiega
Pobiega2y ago
var could be swapped out to SaotomeItem SaotomeItem mb = new SaotomeItem("Pobiega Extreme Motherboard", 12312); does the exact same thing
Saotom Gosling
Saotom GoslingOP2y ago
now my problem is, how do i chose the exact items i added before can i see a list of all added items? ohhhhhhhhhhhhhhhhhhhhhhh
Pobiega
Pobiega2y ago
cb_motheboards.Items
Saotom Gosling
Saotom GoslingOP2y ago
so i can set the value while adding the item?
Pobiega
Pobiega2y ago
the value is what you are adding but you are currently adding a string
Saotom Gosling
Saotom GoslingOP2y ago
yes, but for me the items stuff isnt showing up
Pobiega
Pobiega2y ago
my suggestion is to change that to not be strings, but to add SaotomeItems
Saotom Gosling
Saotom GoslingOP2y ago
makes sense, do i even have proper items?
Pobiega
Pobiega2y ago
Im not sure what you mean by that you are currently only adding strings (names) you can get the name of the selected item, but you have no way of associating the prices (currently)
Saotom Gosling
Saotom GoslingOP2y ago
do these items even have proper names to acces them? or is it just text in them
Pobiega
Pobiega2y ago
its just text
Saotom Gosling
Saotom GoslingOP2y ago
so thats my issue i need to give these items proper names
Pobiega
Pobiega2y ago
well, they are items they have names
Saotom Gosling
Saotom GoslingOP2y ago
well but what are their names?
Pobiega
Pobiega2y ago
but they have ONLY names they are not variables, if thats what you mean
Saotom Gosling
Saotom GoslingOP2y ago
i think thats it, how do i select/work with an item if its just text and i cant contact it i need to name it so i can use it
Pobiega
Pobiega2y ago
no you dont for example, go back to the selectedIndexChanged event handler and try and access cb_motherboard.SelectedItem in there
Saotom Gosling
Saotom GoslingOP2y ago
i changed that to frm load or did i
Pobiega
Pobiega2y ago
hopefully you just moved the code if you renamed the entire event handler, you have some fun bugs to solve
Saotom Gosling
Saotom GoslingOP2y ago
i dont think there was anything to bug in first place it should still work
Pobiega
Pobiega2y ago
you need to understand that the event handlers are just normal methods and that they are "registered" to their respective events in the designer, not by their name
Saotom Gosling
Saotom GoslingOP2y ago
ok i know, i just dont have that indexchanged in my code rn
Pobiega
Pobiega2y ago
then add it thats the event that gets triggered when an item is selected
Saotom Gosling
Saotom GoslingOP2y ago
i need to do that for each combobox right
Pobiega
Pobiega2y ago
ytp yup
Saotom Gosling
Saotom GoslingOP2y ago
got it so im in that selectedindexchanged rn i have these items, but no way to use them
Pobiega
Pobiega2y ago
sure you do
Saotom Gosling
Saotom GoslingOP2y ago
well how
Pobiega
Pobiega2y ago
cb_motherboard.SelectedItem gives you the currently selected item of the motherboard combobox
Saotom Gosling
Saotom GoslingOP2y ago
by god... i am so dumb, i got it yeah i got that but i wanna give these items a value and each selected item has a diffrent value so what do i even want to do rn
Pobiega
Pobiega2y ago
well you need to change from having just strings in your boxes
Saotom Gosling
Saotom GoslingOP2y ago
exactly
Pobiega
Pobiega2y ago
so instead of adding "Gigabyte Whatever" you need to add new HardwareComponent("Gigabyte Whatever", 213125123) (use thinkers class, it makes more sense for a beginner)
Saotom Gosling
Saotom GoslingOP2y ago
YOURE TELLING ME ITS THAT EASY
Pobiega
Pobiega2y ago
kinda its ALMOST that easy 🙂 because now, SelectedItem will not be a string. it will be a HardwareComponent
Saotom Gosling
Saotom GoslingOP2y ago
so when i add the items, i just put a , and the number behind it?
Pobiega
Pobiega2y ago
public HardwareComponent(string name, decimal price) thats the constructor it declares what parameters exist and their order
Saotom Gosling
Saotom GoslingOP2y ago
i finaly understood it thanks god duning kruger hit me hard
Pobiega
Pobiega2y ago
pseudocode:
public class Form1
{
private List<HardwareComponent> _motherboards = new();

private string _motherboardPrice = string.Empty;

public Form1()
{
_motherboards.Add(new HardwareComponent("Saotome Hyperspace MB Extreme", 123));
_motherboards.Add(new HardwareComponent("Pobiega Industries Hightech X-44", 666));
}

public void MotherboardSelectedIndexChanged()
{
if (_motherboards.SelectedItem is not HardwareComponent mb)
{
// this is bad, we did something wrong
throw new InvalidOperationException("Bad code!");
}

_motherboardPrice = mb.Price.ToString();
}
}
public class Form1
{
private List<HardwareComponent> _motherboards = new();

private string _motherboardPrice = string.Empty;

public Form1()
{
_motherboards.Add(new HardwareComponent("Saotome Hyperspace MB Extreme", 123));
_motherboards.Add(new HardwareComponent("Pobiega Industries Hightech X-44", 666));
}

public void MotherboardSelectedIndexChanged()
{
if (_motherboards.SelectedItem is not HardwareComponent mb)
{
// this is bad, we did something wrong
throw new InvalidOperationException("Bad code!");
}

_motherboardPrice = mb.Price.ToString();
}
}
I don't have visual studio installed, and I dont like working with winforms, so this is an aproximation so you would use an actual combobox/textbox instead of list/string here
Saotom Gosling
Saotom GoslingOP2y ago
ok so i gotta do that hardwarecompoennt thing i see otherwise the add doesnt work damn im really braining today god im doing something wrong it doesnt wna tme to add the hardwarecomponent list nvm i looked trough it okay it doesnt want to
Preda
Preda2y ago
I ll launch VS now and make the code for you
Saotom Gosling
Saotom GoslingOP2y ago
i apologize if this makes you vomit
Preda
Preda2y ago
Where is your HardwareComponent class?
Saotom Gosling
Saotom GoslingOP2y ago
excellent question oh yeah i need to learn about classes i think
Pobiega
Pobiega2y ago
for now, just slap it down outside of your frm_specs also, you were not supposed to add the list or the string I made they were just placeholders for your combobox/textbox
Saotom Gosling
Saotom GoslingOP2y ago
okay i get confused with placeholders at times because i cant tell apart from code i dont know wabout
Pobiega
Pobiega2y ago
thats why I said they were placeholders 😛
Saotom Gosling
Saotom GoslingOP2y ago
wand why outside of it? isnt it where everything is in or what do i have to create
Pobiega
Pobiega2y ago
just put it outside for now. nested classes is a mess
Saotom Gosling
Saotom GoslingOP2y ago
i see
Pobiega
Pobiega2y ago
and I mean the class definition you will still fill your comboboxes as before, but with new HardwareComponents
Saotom Gosling
Saotom GoslingOP2y ago
i think i have another issue...
Pobiega
Pobiega2y ago
can we jump on screenshare? doing this over text is... slow and indirect 😛
Saotom Gosling
Saotom GoslingOP2y ago
vc0
Pobiega
Pobiega2y ago
alright I'm back You can /close the thread if you don't have any further questions.
Accord
Accord2y 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