Casting object to enum class
So I have:
And I wanna case my object to that class:
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
If you need more information lemme know
so your end result should be that
var x = (AClass)a; x.GetType() == typeof(int) == true;
?What is the problem you're trying to solve? Or put differently, why do you need to be able to do this?
Yeah my XY sense is tingling.
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:
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>
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
Yeah same
Its really hard to parse what you are trying to do here
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???).no it's not even allowed 🙂
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!
Thats fair :p
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:
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 killYou 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
TextBox
es, 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 objectWhat 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
My bad, I am not used to asking for help
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
Ok
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 usefulThe 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
if you know the type, sure?
if you dont know the type at compile time, you have to rely on
object
or another base-typeAnd 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
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
Serialize the object but you can't change it's type?
allow me to demonstrate
Pobiega
REPL Result: Success
Console Output
Compile: 375.127ms | Execution: 25.224ms | React with ❌ to remove this embed.
do you see how
i
is still an int, even after we "changed its type"?Yeah
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
Ok I see.
So there is no way to actually 'delete'
i
and 'remake' i
as the doublenot 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..
what is a
and a2
here, for you?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?you can't just "take" a string and "make" it be an int. you parse it.
but for lets say an
int
and 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 examplesa2 will have a copy of a?
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?So if a changes, a2 will change?
yes
It will return
a
So
ClassAcorrect
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.So is Base a subtype of ChildA
reverse
ChildA inherits Base, so ChildA is the subtype
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
yeah, but you can't
because you dont know what type it is
and casting is only useful at compile time
you can't call
DoSomething
unless we know the argument is an integerI see. So that means in the end, we need checks for every type before casting
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?Sheesh ok, that is hectic
May i ask why if you don't mind
because at that point, why use C#?
why use a typed language, if you are going to leave the type system behind
That was like a mic drop moment lmao
It is a fair question. 😄
c# is what the entire system is built on, so this issue I face is like something small compared to the entire codebase
I still think this is an XY problem
What exactly is an XY problem?
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
https://xyproblem.info/ - Try to explain what your original problem is, not just what's wrong with your attempted solution.
This actually might be it...damm
It's a very common problem while learning 😛 Easy to tunnelvision on something and lose track of the bigger picture
its incredibly easy to get stuck in XY yeah
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
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.