C
C#ā€¢2y ago
Itskillerluc

ā” 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ā€¢2y ago
Can you give an example? Enums already have instances, one for each value in the enum.
Itskillerluc
ItskillerlucOPā€¢2y 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ā€¢2y ago
Sounds like you want a class more than an enum
Itskillerluc
ItskillerlucOPā€¢2y 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ā€¢2y ago
enums are a compiler trick for the underlying type, so no they can't have different values.
Thinker
Thinkerā€¢2y 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).
Itskillerluc
ItskillerlucOPā€¢2y ago
oh C# has records too nice
TheRanger
TheRangerā€¢2y ago
and structs
Itskillerluc
ItskillerlucOPā€¢2y ago
i mean ye that would work then i guess
Thinker
Thinkerā€¢2y ago
Well depends on what you're comparing them to
Itskillerluc
ItskillerlucOPā€¢2y ago
i knew the structs but i dont see me using structs soon i guess they are just an immutable map
Thinker
Thinkerā€¢2y ago
records are just sugar for classes and structs
TheRanger
TheRangerā€¢2y ago
u can assign values of walk, sneak and print in a struct instance
Thinker
Thinkerā€¢2y ago
That would be more like an ImmutableDictionary
Itskillerluc
ItskillerlucOPā€¢2y ago
sorry java mind, its called a dictionary here XD i was thinking of the same thing xD
Thinker
Thinkerā€¢2y ago
Also, C#'s enums aren't really as powerful as Java's In C#, enums are essentially just integers with names.
Itskillerluc
ItskillerlucOPā€¢2y ago
oh šŸ˜¦ so they arent their own type?
Thinker
Thinkerā€¢2y ago
well yes they are However the underlying data of an enum is just a number So they can't store any other data
Itskillerluc
ItskillerlucOPā€¢2y ago
WAIT you cant have like a constructor in the enum like in java?
Thinker
Thinkerā€¢2y ago
nope Java patented that so now no other languages can use it
Itskillerluc
ItskillerlucOPā€¢2y ago
bruh actually? thats so dumb T-T
Thinker
Thinkerā€¢2y ago
However, we are hopefully getting proper discriminated unions in C# soon-ish
Itskillerluc
ItskillerlucOPā€¢2y ago
idek what that means
TheRanger
TheRangerā€¢2y ago
applying attribute on an enum value is the closest thing u can get probably
Itskillerluc
ItskillerlucOPā€¢2y ago
okay but i still need a value thats like 1 value holding multiple things
Thinker
Thinkerā€¢2y ago
essentially what Java has but even more powerful
Itskillerluc
ItskillerlucOPā€¢2y ago
a class is so bulky tho
Thinker
Thinkerā€¢2y ago
$unionsdiscussion
MODiX
MODiXā€¢2y 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ā€¢2y ago
union, like in c++?
Itskillerluc
ItskillerlucOPā€¢2y ago
enums are static? or not
Thinker
Thinkerā€¢2y ago
union, like in F#
TheRanger
TheRangerā€¢2y ago
never did F#
Thinker
Thinkerā€¢2y ago
look up discriminated unions yes-ish
Itskillerluc
ItskillerlucOPā€¢2y 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ā€¢2y ago
oh this is Unity? forget about records then
Itskillerluc
ItskillerlucOPā€¢2y ago
WHAT wdym but records are cool
Thinker
Thinkerā€¢2y ago
no, enum values are constants
Itskillerluc
ItskillerlucOPā€¢2y ago
well ye i know but then what do i use
Thinker
Thinkerā€¢2y ago
It's a feature from a language version ahead of what Unity uses
Itskillerluc
ItskillerlucOPā€¢2y 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ā€¢2y ago
No idea if you can do that. You'd probably have to ask in $unity.
Itskillerluc
ItskillerlucOPā€¢2y ago
oh
Thinker
Thinkerā€¢2y ago
wdym
Itskillerluc
ItskillerlucOPā€¢2y ago
well im not gonna build a full on 3d renderer and game enigne
Thinker
Thinkerā€¢2y ago
There are other engines which use C#
Itskillerluc
ItskillerlucOPā€¢2y 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ā€¢2y ago
it's not officially supported but you can still use it by adding the namespace manually System.Runtime.CompilerServices.IsExternalInit
TheRanger
TheRangerā€¢2y ago
structs are value types, classes are reference types $refvsvalue
Itskillerluc
ItskillerlucOPā€¢2y ago
ye i created a new thingy for it but i understand
Accord
Accordā€¢2y 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