C
C#16mo 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
Theos16mo 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
Angius16mo ago
Do you want generics, perchance?
Theos
Theos16mo ago
i think so, but I'm not exactly sure how to use them in my case
Angius
Angius16mo 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
Theos16mo ago
what about public string Type;? Cuz for now its a string I cant just make it a public T Type
Angius
Angius16mo ago
So you actually want the type to be a string?
Theos
Theos16mo 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
Angius16mo ago
Why would you pass it as a string though...?
Theos
Theos16mo ago
cuz i'm reading it from a txt file
Angius
Angius16mo ago
Uh Well, maybe you can do some reflections tomfuckery to get an actual type from just it's string name
Theos
Theos16mo ago
well, there is 3 possbile options in my case it can either be a short, long, int so maybe a simple switch?
Angius
Angius16mo ago
I'd say so, yeah A switch expression even
Theos
Theos16mo ago
but then public [what here?] Type;
Angius
Angius16mo 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
Theos16mo ago
alright, I'll get back to it tomorrow and see how it goes thanks
Thinker
Thinker16mo ago
(that should probably be in a static class? otherwise you'd have to call it using MyClass<int>.GetInstance("long"))
Angius
Angius16mo ago
Huh, true So it'd need two classes
Thinker
Thinker16mo ago
Also what's the return type
Angius
Angius16mo ago
Right
Accord
Accord16mo 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.
Want results from more Discord servers?
Add your server