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
SparkyCracked3mo ago
If you need more information lemme know
Pobiega
Pobiega3mo ago
so your end result should be that var x = (AClass)a; x.GetType() == typeof(int) == true;?
Esa
Esa3mo ago
What is the problem you're trying to solve? Or put differently, why do you need to be able to do this?
Pobiega
Pobiega3mo ago
Yeah my XY sense is tingling.
SparkyCracked
SparkyCracked3mo 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
Esa3mo 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
Pobiega3mo ago
Yeah same Its really hard to parse what you are trying to do here
Esa
Esa3mo 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
Esa3mo ago
no it's not even allowed 🙂
No description
SparkyCracked
SparkyCracked3mo 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
Pobiega3mo ago
Thats fair :p
SparkyCracked
SparkyCracked3mo 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
Pobiega3mo 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
SparkyCracked3mo ago
My bad, I am not used to asking for help
Pobiega
Pobiega3mo 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
SparkyCracked3mo ago
Ok
Pobiega
Pobiega3mo 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
SparkyCracked3mo 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
Pobiega3mo 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
SparkyCracked3mo 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
Pobiega3mo 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
SparkyCracked3mo ago
Serialize the object but you can't change it's type?
Pobiega
Pobiega3mo ago
allow me to demonstrate
MODiX
MODiX3mo 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
Pobiega3mo ago
do you see how i is still an int, even after we "changed its type"?
SparkyCracked
SparkyCracked3mo ago
Yeah
Pobiega
Pobiega3mo 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
SparkyCracked3mo ago
Ok I see. So there is no way to actually 'delete' i and 'remake' i as the double
Pobiega
Pobiega3mo 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
SparkyCracked3mo 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
Pobiega3mo 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
SparkyCracked3mo ago
a2 will have a copy of a?
Pobiega
Pobiega3mo 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
SparkyCracked3mo ago
So if a changes, a2 will change?
Pobiega
Pobiega3mo ago
yes
SparkyCracked
SparkyCracked3mo ago
It will return a So ClassA
Pobiega
Pobiega3mo 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
SparkyCracked3mo ago
So is Base a subtype of ChildA
Pobiega
Pobiega3mo ago
reverse ChildA inherits Base, so ChildA is the subtype
SparkyCracked
SparkyCracked3mo 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
Pobiega3mo 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
SparkyCracked3mo ago
I see. So that means in the end, we need checks for every type before casting
Pobiega
Pobiega3mo 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
SparkyCracked3mo ago
Sheesh ok, that is hectic May i ask why if you don't mind
Pobiega
Pobiega3mo ago
because at that point, why use C#? why use a typed language, if you are going to leave the type system behind
SparkyCracked
SparkyCracked3mo ago
That was like a mic drop moment lmao
Esa
Esa3mo ago
It is a fair question. 😄
SparkyCracked
SparkyCracked3mo 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
Pobiega3mo ago
I still think this is an XY problem
SparkyCracked
SparkyCracked3mo ago
What exactly is an XY problem?
Pobiega
Pobiega3mo 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
MODiX3mo ago
https://xyproblem.info/ - Try to explain what your original problem is, not just what's wrong with your attempted solution.
SparkyCracked
SparkyCracked3mo ago
This actually might be it...damm
Esa
Esa3mo ago
It's a very common problem while learning 😛 Easy to tunnelvision on something and lose track of the bigger picture
Pobiega
Pobiega3mo ago
its incredibly easy to get stuck in XY yeah
SparkyCracked
SparkyCracked3mo 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
Esa3mo 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
More Posts