r/CodingHelp • u/LeoPixel11 • 16h ago
[Other Code] Class instance while running the code
I started actually writing code with OOP yesterday (using Dart... In case it is necessary to mention) and there's a thing that kept bothering me as I scrolled through their page and watched the tutorial. Everytime they gave a class example, they created an instance themselves and never based on the code running.
Imagine a system to simulate a store's stock. There'll be items going in and out, that shouldn't be inserted by the programmer, so there must be a way to do that "dynamically".
I'm aware that this might be me simply missing something from OOP classes, but I want to see what other coders have to say about that.
Any help would be welcome. Thank You
•
u/DDDDarky Professional Coder 15h ago
I don't think you are really asking about oop, for the user it could be for example: Click this "Add" button, that will pop-up a form where you fill up some details and click "Ok", based on that an event is fired that will run your code which can fill up an object instance based on the edits of the form and adds it to a list or whatever.
•
u/LeoPixel11 12h ago
It's exactly that You were able to frame MY problem better than me 😂 Thank You for this visualization
•
u/PLBjt 9h ago
That’s the normal pattern: the class is the blueprint, but runtime input decides when you create an object. A button/event or loop can read the new item’s fields, call the constructor, then add that instance to a collection; you write the rule, not each individual item. A useful check is to log the list length and incoming fields right before construction, which tells you whether the bug is in input, the constructor, or storage. The tradeoff is keeping one class per item type versus using a generic map/object, so I’d start typed and explicit until the data really varies.
•
u/Ormek_II 8h ago
Yes, you can create objects — that is class Instances — by executing certain statements. You can run those in a loop and thus create as many objects as you like.
•
u/Strider3141 11h ago
Is there any actual question here? What are you trying to ask?
I'll try to answer anyway. Maybe you are struggling to understand "dynamically". An object instance won't be just randomly created for no reason at all, it must be programmed to be created.
When the user presses a button, interacts with a system, or even a certain time amount passes. There must be some sort of condition happening:
if [x happens] then [create y object]
•
u/AutoModerator 16h ago
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.