Casting object to enum class

So I have:
enum AClass { double, int, string }
enum AClass { double, int, string }
And I wanna case my object to that class:
obj a = 0;
(AClass)a;
obj a = 0;
(AClass)a;
There is a reflector to determine what variable type it is but it kinda does this on runtime/compile. I wanna know if there is a way to cast this without wrapping it
57 Replies
SparkyCracked
SparkyCrackedOP9mo ago
If you need more information lemme know
Pobiega
Pobiega9mo ago
so your end result should be that var x = (AClass)a; x.GetType() == typeof(int) == true;?
Esa
Esa9mo ago
What is the problem you're trying to solve? Or put differently, why do you need to be able to do this?
Pobiega
Pobiega9mo ago
Yeah my XY sense is tingling.
SparkyCracked
SparkyCrackedOP9mo ago
So it's more before var x = (AClass)a; I wanna pick up what the type is before Examples of this is a function right:
public T Person <T> (int id){
return this ->
}
public T Person <T> (int id){
return this ->
}
This knows what the type is...so I wanna do the same in the sense of having my variables almost 'regexed' into the type of variable it it, break up the character and put it into it's type (double) a double happens on runtime though which isn't what I want @Fyren @Pobiega Wanna try avoid a function to do this, I'm sure there is a way with c# cause they do it, although with that public person, T is defined when you call it person1 = Person<int>
Esa
Esa9mo ago
Okay, you're discussing implementation details for a problem you've yet to define. I'd like to help you but personally I don't understand what it is that you are trying to solve
Pobiega
Pobiega9mo ago
Yeah same Its really hard to parse what you are trying to do here
Esa
Esa9mo ago
So your AClass is an enum. When you try casting to an AClass, it'll never be casted to double, int or string, because those are just values in your AClass enum (is that even allowed???).
Esa
Esa9mo ago
no it's not even allowed 🙂
No description
SparkyCracked
SparkyCrackedOP9mo ago
Ok lemme come back tomorrow with this. It's something I can't put into words right now myself as well. Thats the best I can describe it myself right now... I'll be back!
Pobiega
Pobiega9mo ago
Thats fair :p
SparkyCracked
SparkyCrackedOP9mo ago
So @Pobiega @Fyren I have returned After long thought I have come up with this: The problem put into something I can explain looks like this: We have text box's right, they get instantiated/defines as strings on build, and on runtime, they stay as strings. Thats what they are. Now taking the object a, my goal is to change/define what type it is during run time, after build. So taking what you gave, it needs to happen before this: var x = (AClass)a; I thought of casting the object and using as, so like:
c#
(AClass)a
// or
(a as AClass)
c#
(AClass)a
// or
(a as AClass)
But this doesn't really do what I want. My AClass already has a reflector that will find what type it is, I just need a way to change the object (the text box for example, from an object to a double, or to an int) This obviously can be done with if statements and a function but I have like 500+ (overexaggerated number but accurate ^^) types of variables...and creating if statements or functions for each (double) a double would kill
Pobiega
Pobiega9mo ago
You use the weirdest terms for things, makes it really hard to understand what you are trying to say.
We have text box's right, they get instantiated/defines as strings on build, and on runtime, they stay as strings.
No, they are TextBoxes, not strings. They have a .Text property that is a string thou sure. If you want to do runtime typing, you will be using object as your declared type and casting/as
My AClass already has a reflector that will find what type it is, I just need a way to change the object
What does this mean? your class has a reflector? do you mean a type discriminator field? and "change the object"? your object is what it is
SparkyCracked
SparkyCrackedOP9mo ago
My bad, I am not used to asking for help
Pobiega
Pobiega9mo ago
you literally can't change the type of an object. You can change the type of the variable referencing the object. But only to types known at compile time - otherwise you have to rely on polymorphism
SparkyCracked
SparkyCrackedOP9mo ago
Ok
Pobiega
Pobiega9mo ago
you could use public static object? ChangeType (object? value, Type conversionType); but honestly I dont really see the point, as you'd still need to cast the result for it to be useful
SparkyCracked
SparkyCrackedOP9mo ago
The problem I face is with casting Nvm, I think imma just try solve another issue before this one, it's something I don't know at all and was just speculating if it was a possibly. With code anything is possible so I'll just need to build a wider understanding of code before this...I truly appreciate your help. If I somehow get to understand what I wanna do better, I'll ping you guys <3 So just for conversation purposes, if on compile time, my program has those classes, and the object gets declared, I can cast the object to that class
Pobiega
Pobiega9mo ago
if you know the type, sure? if you dont know the type at compile time, you have to rely on object or another base-type
SparkyCracked
SparkyCrackedOP9mo ago
And now it works different with runtime right... Yes this... I don't know the type on compile time...only on runtime will I know what it will be
Pobiega
Pobiega9mo ago
And that's fine, but it means you can't get strict type checking for anything you do with that object If all you do is serialize it, that's fine But if you want to do actual business logic on it, it gets messy
SparkyCracked
SparkyCrackedOP9mo ago
Serialize the object but you can't change it's type?
Pobiega
Pobiega9mo ago
allow me to demonstrate
MODiX
MODiX9mo ago
Pobiega
REPL Result: Success
object i = 52;
Console.WriteLine(i.GetType().Name);
object d = Convert.ChangeType(i, typeof(double));
Console.WriteLine(i.GetType().Name);
Console.WriteLine(d.GetType().Name);
object i = 52;
Console.WriteLine(i.GetType().Name);
object d = Convert.ChangeType(i, typeof(double));
Console.WriteLine(i.GetType().Name);
Console.WriteLine(d.GetType().Name);
Console Output
Int32
Int32
Double
Int32
Int32
Double
Compile: 375.127ms | Execution: 25.224ms | React with ❌ to remove this embed.
Pobiega
Pobiega9mo ago
do you see how i is still an int, even after we "changed its type"?
SparkyCracked
SparkyCrackedOP9mo ago
Yeah
Pobiega
Pobiega9mo ago
thats what I mean with "you cant change an objects type, only the type of the reference to the object" we can ofc create a NEW object with the value of another object and thats whats actually being done here
SparkyCracked
SparkyCrackedOP9mo ago
Ok I see. So there is no way to actually 'delete' i and 'remake' i as the double
Pobiega
Pobiega9mo ago
not as such, but you could stop caring about i and it would get garbage collected eventually there is another level of complexity here with value types vs reference types int etc is a value type, which means unless you are using unsafe or ref, its "pass by value" and copies the value also, just to be clarify a few things..
ChildA a = new ChildA();
Base a2 = a;

class Base { }
class ChildA : Base { }
ChildA a = new ChildA();
Base a2 = a;

class Base { }
class ChildA : Base { }
what is a and a2 here, for you?
SparkyCracked
SparkyCrackedOP9mo ago
I don't fully understand this From what I am getting, the value is copied and then it is then written as a new type? so value '32' as a string will then be taken to 32? I'm assuming this is like it's hex code?
Pobiega
Pobiega9mo ago
you can't just "take" a string and "make" it be an int. you parse it. but for lets say an intand a long, you can literally just take the value and treat it like a different type because they are both integer types, with the exact same bit layout a long just has a larger size so any valid int value, is a valid long value another problem with your entire setup is that you have never explained what a is in your examples
SparkyCracked
SparkyCrackedOP9mo ago
a2 will have a copy of a?
Pobiega
Pobiega9mo ago
no, these are classes, so they are reference types a2 is just another reference to the exact same object what would a2.GetType().Name return?
SparkyCracked
SparkyCrackedOP9mo ago
So if a changes, a2 will change?
Pobiega
Pobiega9mo ago
yes
SparkyCracked
SparkyCrackedOP9mo ago
It will return a So ClassA
Pobiega
Pobiega9mo ago
correct the runtime type is still ChildA but the reference type is Base when we cast an object with (T) etc, all we are doing is changing the reference type, with a few exceptions.
SparkyCracked
SparkyCrackedOP9mo ago
So is Base a subtype of ChildA
Pobiega
Pobiega9mo ago
reverse ChildA inherits Base, so ChildA is the subtype
SparkyCracked
SparkyCrackedOP9mo ago
Fair Ahhh ok I see Base a2 -> a -> childA -> childA subtype base -> instance of Base Ok I'm here now, sorry for the detour treat is the keyword here I see. So it isn't another type, but we treating it like one it's just an object Just wanna cast it to whatever type it is
Pobiega
Pobiega9mo ago
yeah, but you can't because you dont know what type it is and casting is only useful at compile time
object i = 52;

int x = i; // error
int y = (int)i; // yay!

DoSomething(i); // error!
DoSomething(y); // yay!

void DoSomething(int integer)
{
//...
}
object i = 52;

int x = i; // error
int y = (int)i; // yay!

DoSomething(i); // error!
DoSomething(y); // yay!

void DoSomething(int integer)
{
//...
}
you can't call DoSomething unless we know the argument is an integer
SparkyCracked
SparkyCrackedOP9mo ago
I see. So that means in the end, we need checks for every type before casting
Pobiega
Pobiega9mo ago
either that, or you need methods that accept object and typecheck internally. I DO NOT recommend that thou also, how come a is just an object in the first place? how did it enter your system untyped?
SparkyCracked
SparkyCrackedOP9mo ago
Sheesh ok, that is hectic May i ask why if you don't mind
Pobiega
Pobiega9mo ago
because at that point, why use C#? why use a typed language, if you are going to leave the type system behind
SparkyCracked
SparkyCrackedOP9mo ago
That was like a mic drop moment lmao
Esa
Esa9mo ago
It is a fair question. 😄
SparkyCracked
SparkyCrackedOP9mo ago
c# is what the entire system is built on, so this issue I face is like something small compared to the entire codebase
Pobiega
Pobiega9mo ago
I still think this is an XY problem
SparkyCracked
SparkyCrackedOP9mo ago
What exactly is an XY problem?
Pobiega
Pobiega9mo ago
Your problem is X. When you thought about it, you decided that solution Y is the way to go. So you ask for help with Y $xyproblem
MODiX
MODiX9mo ago
https://xyproblem.info/ - Try to explain what your original problem is, not just what's wrong with your attempted solution.
SparkyCracked
SparkyCrackedOP9mo ago
This actually might be it...damm
Esa
Esa9mo ago
It's a very common problem while learning 😛 Easy to tunnelvision on something and lose track of the bigger picture
Pobiega
Pobiega9mo ago
its incredibly easy to get stuck in XY yeah
SparkyCracked
SparkyCrackedOP9mo ago
Yeah, I tend to do it a lot. Any tips on breaking this? @Pobiega @Fyren I truly appreciate your help and patience with me. Thank you
Esa
Esa9mo ago
I think it's just a process tbh. Do try to be aware if you're tunnelling in on some topic, seemingly stuck. It may help to "zoom out" and try another approach now and then.
Want results from more Discord servers?
Add your server