❔ 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
Make each item a custom class containing a name and a price instead of just a string.
ok, may you share the command with me? pls
i am asking respectfully
i am just a person in need
Do you know how to make a class?
im new
is it just class.new
meh
Is this part of an assignment or something?
trying to do something by myself
whenever i dont know something i usualy look at my other projects
right
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
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))
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
You won't learn (as much) if someone just gives you the code
i know, i will look into it. I learn quicker by looking at something
i again apologize for myself
But sure, here's a quick example
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();
it's fine
show your code, what errors are you getting?
$prettycode
To format your code in Visual Studio, Visual Studio Code, Rider, use the following shortcut:
NOTE: the first key must be held while doing it.
https://cdn.discordapp.com/attachments/569261465463160900/899513918567890944/2021-10-18_01-26-35.gif
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
well
uhm
so the } appears to be a problem aswell for a reason
Count your
{
s and }
s
And see if their numbers matchIt looks like you've put the class inside a method, which isn't right.
yeah i forgot on }
Also the
List<HardwareComponent> items = new();
stuff was just an exampleits currently in public partial class frm_Specs : Form
ah
ohhhhhhh
i got it now
uhm
well
maybe i dont
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.get set still is damn complicated
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.
Shouldn t it be ?
No
I mean, it can be, but why?
I have never used only what is diffrent?
List<HardwareComponent> Items = new List<HardwareComponent>();
and List<HardwareComponent> Items = new();
are literally the same
except the second is shorterthinker227#5176
REPL Result: Success
Result: List<int>
Compile: 514.313ms | Execution: 27.219ms | React with ❌ to remove this embed.
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.I GOT AN IDEA FINALLY
its just like excel
nevermind
@Officer Saotome u still need help?
yep, i really wanna learn how to do it simply
i attempted figuring out a new way
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
i am trying to make things edible
i dont know if my idea is to complicated but what i want ot do makes sense to me
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"
why that
ohh i see
but it doesnt make much of a difference for the enduser, i can place that code somewhere else sure
Its a Drop Down List
it makes a lot of difference
every time I select something, you add 3 new duplicate options
so i just copy that items outside of that event in the load for example?
are they just supposed to be the default items?
in that case, I suggest adding them via the designer, or in the constructor.
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
i placed them in the frm load, thats better right?
ye
then its a one time event, instead of happening multiple times
good
You can create a list in the form (when form is strated create a list)
uhm well i dont know how to do a list, i work with what i know right now
keep it as is for now
even if it becomes a mess, i wanna expand this programm little for little
Now, if I understand your question correctly, you want to associate prices with these items, yes?
yes, one sec ima try to give a visual example
each MB having its own price, each cpu having its own price etc
right
have you learnt about classes and/or records yet?
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
I understand
combobox.SelectedValue
and i got plenty ideas from that
have you learnt about classes and/or records yet?
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
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);
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;
}
we can then create an instance of that like so:
var mb = new SaotomeItem("Pobiega Extreme Motherboard", 12312);
When adding things to a DDL normally it would assign the value
yep, thats pretty much the same thing
var, well i gotta figure that out
what does the letters after var mean
cause im trying to undeerstand every input i get
mb
? thats just a variable
variable name, I should sayi see
oh i think i understand
var
could be swapped out to SaotomeItem
SaotomeItem mb = new SaotomeItem("Pobiega Extreme Motherboard", 12312);
does the exact same thingnow my problem is, how do i chose the exact items i added before
can i see a list of all added items?
ohhhhhhhhhhhhhhhhhhhhhhh
cb_motheboards.Items
so i can set the value while adding the item?
the value is what you are adding
but you are currently adding a string
yes, but for me the items stuff isnt showing up
my suggestion is to change that to not be strings, but to add
SaotomeItem
smakes sense, do i even have proper items?
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)
do these items even have proper names to acces them? or is it just text in them
its just text
so thats my issue
i need to give these items proper names
well, they are items
they have names
well but what are their names?
but they have ONLY names
they are not variables, if thats what you mean
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
no
you dont
for example, go back to the
selectedIndexChanged
event handler
and try and access cb_motherboard.SelectedItem
in therei changed that to frm load
or did i
hopefully you just moved the code
if you renamed the entire event handler, you have some fun bugs to solve
i dont think there was anything to bug in first place
it should still work
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
ok i know, i just dont have that indexchanged in my code rn
then add it
thats the event that gets triggered when an item is selected
i need to do that for each combobox right
ytp
yup
got it
so im in that selectedindexchanged rn
i have these items, but no way to use them
sure you do
well how
cb_motherboard.SelectedItem
gives you the currently selected item
of the motherboard comboboxby 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
well you need to change from having just strings in your boxes
exactly
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)YOURE TELLING ME ITS THAT EASY
kinda
its ALMOST that easy 🙂
because now,
SelectedItem
will not be a string. it will be a HardwareComponent
so when i add the items, i just put a , and the number behind it?
public HardwareComponent(string name, decimal price)
thats the constructor
it declares what parameters exist and their orderi finaly understood it thanks god
duning kruger hit me hard
pseudocode:
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
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
I ll launch VS now and make the code for you
i apologize if this makes you vomit
Where is your HardwareComponent class?
excellent question
oh yeah i need to learn about classes i think
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/textboxokay i get confused with placeholders at times because i cant tell apart from code i dont know wabout
thats why I said they were placeholders 😛
wand why outside of it? isnt it where everything is in or what do i have to create
just put it outside for now. nested classes is a mess
i see
and I mean the class definition
you will still fill your comboboxes as before, but with
new HardwareComponent
si think i have another issue...
can we jump on screenshare? doing this over text is... slow and indirect 😛
vc0
alright I'm back
You can
/close
the thread if you don't have any further questions.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.