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