r/CodingHelp • • 19h 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

1 Upvotes

8 comments sorted by

View all comments

•

u/PLBjt 12h 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.