Can I have a `ListThingT ` where T is different for each item?
Can I have a
List<Thing<T>>
where T is different for each item?37 Replies
No. I always think of this problem this way, how do you plan on using the objects in the list? Is there common functionality of Thing that doesn't depend on T? If so, there should be a non-generic Thing base class
Nope, why do you think you need it?
I want the user to be able to create objects and the properties that will be on them. So I might create an object called recipe and give it properties of string difficulty, double time etc
then I could also create another thing called Ingredient with it's own properties and make that a property on the Recipe thing
You want those definable by the user at runtime?
Yeah
So you want to replicate the type system at runtime. An oft desired thing.
Yeah basically I just want to create a skeleton of an app that let users define their own entities to track.
Well you can always use object
Plus maybe store the type of the object in the list too, so you can cast it
It's not an easy thing to do in c# and there's no easy solutions for anything when you're trying to essentially map the type system onto a db
Well I just thought if it's going in a DB then probably everything will just be stored as strings. So type will be a string like "double"/"number" and the value would be "1.5" or something. I'll just have some methods for getting back the value as a double.
Your going to have to store much more than just the value, even for numerics.
1.054 means different things in different locales
1.054 means different things in different locales
That's fine 😄 it's just going to be for me really. Maybe some people I know might use it.
I just want to make one app for managing all things in my life without having to write code for them all 😄
Don't we all
Dictionary<Type, List<object>>
<a:gpa_pepe_kekno:985632411629010944>I've been playing with an app called Home assistant recently and it has dynamic entities
It's like the first time you think you want to individually track every value in the database
as long as you can define a reasonable enough API through interfaces and what you're trying to do isn't too insane
List<object>
and pattern matching is somewhat viable
but yeah generally don't do thatJust use json probably, just serilize/deserilize easy
That would make things like storing a Thing as a property easier yeah
Well as you need to store your object, just use JavaScript object notation (aka. json) as it is literaly made for storing objects
better to use something like MongoDb for that?
Isn't mongodb just db that stores JSONs?
Yeah
So sounds perfect right?
So yes thats something i would propose
Nice, finally a change from SQL server 😄
Well im pretty sure this is the easyest part of the project 😅
Btw are you going to make this opensource? I would like to look at the code/maybe contribute
Yeah I guess if I actually get anywhere with it. I pretty much start a new project every week these days 😄
Wait as i think about it you maybe don't need to actually create objects, you can probably just do with parsing strings, as you just want to read/write properties on the entities right?
I want the user to be able to create their own entity types and the properties.
Ok i might have idea for clean solution: you would need something like this for template of certain entity, for example ingredience:
And if you reference your type somewhere, you just fill the id of the entity you want there in the datatype field instead of primitive type
Yeah that's the kind of thing I am thinking too
Than for the actual objects just take the properties and let user fill in the values (you will need to keep the id somewhere so you know what type are you dealing with)
Sounds good. I am off to bed now so will have a look some more tomorrow. Thanks for your advice 🙂
Np im going to bed too
One thing to consider is that if the code is going to run on users device, it probably is easyer to store it in plain file/files
True, not much need for a server and db
Yep this is the kind of thing where i think that having the files stored on client side is better and much much easier