❔ Pass variable type as class parameter
Hey, so basically I have a class
What I want to do is if I do this for example
Number.Type shouldnt be string but rather a
long
;
and Number.Value should contain the value20 Replies
Later I want to add a simple check to see if
value
fits the type
so for example 100000000
would be too big for a short
etcDo you want generics, perchance?
i think so, but I'm not exactly sure how to use them in my case
IParsable
And .TryParse()
Something like thatwhat about
public string Type;
?
Cuz for now its a string
I cant just make it a public T Type
So you actually want the type to be a string?
when i create a class i pass a type as a string
but in the class
I want to store the actual type
no a string
Why would you pass it as a string though...?
cuz i'm reading it from a txt file
Uh
Well, maybe you can do some reflections tomfuckery to get an actual type from just it's string name
well, there is 3 possbile options in my case
it can either be a
short, long, int
so maybe a simple switch?I'd say so, yeah
A switch expression even
but then
public [what here?] Type;
You want the string type to be passed in the constructor?
If so, I'd use a factory method instead
Something like this
You wouldn't need to store a type even
alright, I'll get back to it tomorrow and see how it goes
thanks
(that should probably be in a static class? otherwise you'd have to call it using
MyClass<int>.GetInstance("long")
)Huh, true
So it'd need two classes
Also what's the return type
Right
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.