C
C#•3y ago
Lvgraef

ā” instanciable enum?

i know C# has all these cool tricks, does it have enum instances? i know it sounds dumb, but i mean like Walking sprinting and sneaking have diff speed values for each creature but each creature only has walk sneaking and sprinting making a single enum perfect
53 Replies
Thinker
Thinker•3y ago
Can you give an example? Enums already have instances, one for each value in the enum.
Lvgraef
LvgraefOP•3y ago
well yes, but what i mean is more like, you have an enum called movement, which holds Sprint, Walk and Sneak, with values Sprint, 5 Walk 3, Sneak 1 there is also a cheeta which has the same enum but Sprint 30 Walk 15 and Sneak 3 and im wondering if there is a neat way of diong that im quite new to C# and so far im amazed with all the neat little things it has
Thinker
Thinker•3y ago
Sounds like you want a class more than an enum
Lvgraef
LvgraefOP•3y ago
i guess a switch would work too but well i already have a class XD the thing is i could make a class but then i would have to create seperate instances for sneaking sprinting and walking
Axiss
Axiss•3y ago
enums are a compiler trick for the underlying type, so no they can't have different values.
Thinker
Thinker•3y ago
You could have a Stats class, sort of like this (using a record for brevity):
record Stats(int Walk, int Sneak, int Sprint);
record Stats(int Walk, int Sneak, int Sprint);
Then when creating a cheetah you could give it a new Stats(3, 1, 5).
Lvgraef
LvgraefOP•3y ago
oh C# has records too nice
TheRanger
TheRanger•3y ago
and structs
Lvgraef
LvgraefOP•3y ago
i mean ye that would work then i guess
Thinker
Thinker•3y ago
Well depends on what you're comparing them to
Lvgraef
LvgraefOP•3y ago
i knew the structs but i dont see me using structs soon i guess they are just an immutable map
Thinker
Thinker•3y ago
records are just sugar for classes and structs
TheRanger
TheRanger•3y ago
u can assign values of walk, sneak and print in a struct instance
Thinker
Thinker•3y ago
That would be more like an ImmutableDictionary
Lvgraef
LvgraefOP•3y ago
sorry java mind, its called a dictionary here XD i was thinking of the same thing xD
Thinker
Thinker•3y ago
Also, C#'s enums aren't really as powerful as Java's In C#, enums are essentially just integers with names.
Lvgraef
LvgraefOP•3y ago
oh 😦 so they arent their own type?
Thinker
Thinker•3y ago
well yes they are However the underlying data of an enum is just a number So they can't store any other data
Lvgraef
LvgraefOP•3y ago
WAIT you cant have like a constructor in the enum like in java?
Thinker
Thinker•3y ago
nope Java patented that so now no other languages can use it
Lvgraef
LvgraefOP•3y ago
bruh actually? thats so dumb T-T
Thinker
Thinker•3y ago
However, we are hopefully getting proper discriminated unions in C# soon-ish
Lvgraef
LvgraefOP•3y ago
idek what that means
TheRanger
TheRanger•3y ago
applying attribute on an enum value is the closest thing u can get probably
Lvgraef
LvgraefOP•3y ago
okay but i still need a value thats like 1 value holding multiple things
Thinker
Thinker•3y ago
essentially what Java has but even more powerful
Lvgraef
LvgraefOP•3y ago
a class is so bulky tho
Thinker
Thinker•3y ago
$unionsdiscussion
MODiX
MODiX•3y ago
GitHub
[LDM] - Union Types Ā· dotnet csharplang Ā· Discussion #7010
The following is a summary of discussions held on discriminated unions in C# by members of the C# language design team. It is not a proposal, and it is not the actual meeting notes. Union Types Uni...
TheRanger
TheRanger•3y ago
union, like in c++?
Lvgraef
LvgraefOP•3y ago
enums are static? or not
Thinker
Thinker•3y ago
union, like in F#
TheRanger
TheRanger•3y ago
never did F#
Thinker
Thinker•3y ago
look up discriminated unions yes-ish
Lvgraef
LvgraefOP•3y ago
can i have an enum that gets its values from a variable? so you can set it in the unity editor well i guess if the enum shows up that would work too but i dont think it does
Thinker
Thinker•3y ago
oh this is Unity? forget about records then
Lvgraef
LvgraefOP•3y ago
WHAT wdym but records are cool
Thinker
Thinker•3y ago
no, enum values are constants
Lvgraef
LvgraefOP•3y ago
well ye i know but then what do i use
Thinker
Thinker•3y ago
It's a feature from a language version ahead of what Unity uses
Lvgraef
LvgraefOP•3y ago
i dont have to change the enum after pressing play, i just want to be able to change it in the unity editor bruh well its imposible to do anythign without unity
Thinker
Thinker•3y ago
No idea if you can do that. You'd probably have to ask in $unity.
Lvgraef
LvgraefOP•3y ago
oh
Thinker
Thinker•3y ago
wdym
Lvgraef
LvgraefOP•3y ago
well im not gonna build a full on 3d renderer and game enigne
Thinker
Thinker•3y ago
There are other engines which use C#
Lvgraef
LvgraefOP•3y ago
wait whats the difference between a class and a struct? sorry for these dumb questions but you dont have structs in java
Cattywampus
Cattywampus•3y ago
it's not officially supported but you can still use it by adding the namespace manually System.Runtime.CompilerServices.IsExternalInit
TheRanger
TheRanger•3y ago
structs are value types, classes are reference types $refvsvalue
Lvgraef
LvgraefOP•3y ago
ye i created a new thingy for it but i understand
Accord
Accord•3y 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?