Is there a 'foreach' equivalent for parts of a class?
I have a class (Stored) defined that has strings stored (Name, ID, and Data, then some others I havent settled on a name for). Many of these are going to be integers stored as strings and would like to be able to go down each element whenever a Stored object is inputted and format them in a certain way if they are actually a number.
I'm not familiar with how foreach works but from what I've read it can't be used for classes - is there an equivalent I can use to accomplish this?
29 Replies
So to be clear, you have a class with several string properties and you want to iterate over those properties?
Yes, rather than manually changing them one at a time (having to repeat an if statement each time), I essentially just write it the once.
What you are asking for is "dynamic" code. In general you want to avoid dynamic code. There is almost always a better design thatn using dynamic code. There are many reasons for this, but I'll just leave it at that unless you want more info.
Having said that, if you hate you life and still want to use dynamic code... one option is to use reflection
output:
there is an example, but again... you shouldn't use reflection general
another option is to make a source generator
It is way easier to just bite the bullet and write a method to do whatever you want to each property individually.
Action<string>
might be of use if you want to apply the same operation to all the properties.but that is rather complex and I would not recommend source generators for beginners
if your code is verbose, then you can likely clean it up. can you share your current code and we might be able to help you?
>integers stored as strings
š
Let me guess, JS background?
I'm afraid I don't understand how it would be damning? My efforts at the moment are basically equivalent to going through and doing a .ToUpper() for each one if it's a certain length
as you get more experience you will understand why using dynamic code is bad
but for now, can you share your current code?
we can probably help you clean up your current code
I'm away from my computer at the moment, I can later though
For reference, how do I format my message to appear like this?
$codegif
I am aware that a foreach style system would need to be sure to not edit Data as its a part of the class
perhaps you should just embrace the type system more? š
Pobiega
REPL Result: Success
Console Output
Compile: 739.703ms | Execution: 104.325ms | React with ā to remove this embed.
would something like this work?
could actually even be a struct
record struct
to get rid of the ctor and property šsick
sorry, my brain is full of snot atm š
@Goblini , have a look
That's neat, but I would still have to go through each individual element, wouldnt I?
what do you mean by go through each element?
The items ID, Name, Turns, etc.
yes, those are the properties but what is your usecase for iterating through them
simply doing ID.ToString(), Name.ToString() etc would give you what you want
Yes but I would need to enter that manually for each property, I'm asking if there's a way to input a class and perform the function for each property, regardless of how many there are or what their names are.
then you are back to reflection
^
Ah, so there isn't a more efficient way than doing it manually
needs tweaking for what you want but that's the general idea
nope
Thanks
if you want to access some random amount of properties without knowing their names, definitely reflection
Alternatively, use a source generator so you don' pay the performance price of using reflections