C
C#2y ago
Theos

❔ Pass variable type as class parameter

Hey, so basically I have a class
class Number
{
public string Type;
public string Value;

public Number(string type, string value)
{
Type = type;
Value = value;
}
}
class Number
{
public string Type;
public string Value;

public Number(string type, string value)
{
Type = type;
Value = value;
}
}
What I want to do is if I do this for example
var num = new Number("long", "10000");
var num = new Number("long", "10000");
Number.Type shouldnt be string but rather a long; and Number.Value should contain the value
20 Replies
Theos
TheosOP2y ago
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 etc
Angius
Angius2y ago
Do you want generics, perchance?
Theos
TheosOP2y ago
i think so, but I'm not exactly sure how to use them in my case
Angius
Angius2y ago
IParsable And .TryParse()
public bool Check<T>(string val) where T : IParsable<T>
{
return T.TryParse(val, out var _);
}
public bool Check<T>(string val) where T : IParsable<T>
{
return T.TryParse(val, out var _);
}
Something like that
Theos
TheosOP2y ago
what about public string Type;? Cuz for now its a string I cant just make it a public T Type
Angius
Angius2y ago
So you actually want the type to be a string?
Theos
TheosOP2y ago
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
Angius
Angius2y ago
Why would you pass it as a string though...?
Theos
TheosOP2y ago
cuz i'm reading it from a txt file
Angius
Angius2y ago
Uh Well, maybe you can do some reflections tomfuckery to get an actual type from just it's string name
Theos
TheosOP2y ago
well, there is 3 possbile options in my case it can either be a short, long, int so maybe a simple switch?
Angius
Angius2y ago
I'd say so, yeah A switch expression even
Theos
TheosOP2y ago
but then public [what here?] Type;
Angius
Angius2y ago
You want the string type to be passed in the constructor? If so, I'd use a factory method instead
public MyClass<T>
{
public T Value { get; set; }
private MyClass<T>(){}

public static GetInstance(string type)
{
return type switch {
"int" => new MyClass<int>(),
"long" => new MyClass<long>(),
"short" => new MyClass<short>(),
_ => throw new Exception("Unsupported type")
};
}
}
public MyClass<T>
{
public T Value { get; set; }
private MyClass<T>(){}

public static GetInstance(string type)
{
return type switch {
"int" => new MyClass<int>(),
"long" => new MyClass<long>(),
"short" => new MyClass<short>(),
_ => throw new Exception("Unsupported type")
};
}
}
Something like this You wouldn't need to store a type even
Theos
TheosOP2y ago
alright, I'll get back to it tomorrow and see how it goes thanks
Thinker
Thinker2y ago
(that should probably be in a static class? otherwise you'd have to call it using MyClass<int>.GetInstance("long"))
Angius
Angius2y ago
Huh, true So it'd need two classes
Thinker
Thinker2y ago
Also what's the return type
Angius
Angius2y ago
Right
Accord
Accord2y ago
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.

Did you find this page helpful?