how to set enum type based on what child class is used
I have the public enum
and I'm making a method to set the enum based on which child class is called. So if class1 is called it will set the enum to worker, if class2 (child of class 1 with inherited variables) it sets to associate etc. I want to set my method up like
but my issue is that im not sure how to set up my if statement so that the class is checked is class1 or not.
7 Replies
Not sure I follow exactly, but one way to solve this is to have your classes specify the enum
lemme show an example
you can replace the interface with a baseclass, if you want
I think I understand now, I'm going to try inheriting WorkerType to all three of my classes and then create a different set method for each class. Thank you for the example
hm? no
thats.. thats not at all what I suggested :p
ok wait Im probably confused then
do you come from java or JS btw?
your naming conventions and bracket placements seem to indicate that
neither of those languages have what we in C# call "properties"
which are just a fancy syntax for
getX
and setX
from Java
WorkerType WorkerType { get; }
this line in the interface says...
Any class implementing this interface MUST have a public WorkerType WorkerType
property that at least has a getter.
its allowed to have a setter too, but its not enforced here
is an example class that implements that interface, and that there is the property and its getter
in Java, this would be...
alright so I think you definitely answered my question on how to set the class based on what child class its in. but I am like really new to this so I think I'm going to need to watch some videos on interface in c# because a quick glance over microsofts website isn't working for me rn. if i close this ticket will it still be accessible or archived somewhere
you can search it up if you remember the title, or copy the link
(https://discord.com/channels/143867839282020352/1088947129847906384)
the interface isnt a must here, as said, you can use a baseclass instead.
its just a way to have "child classes" in this case
and guarantee that they all have the property we want
is also valid, but I personally don't like using base classes where they are not strictly needed
(inheritance has many downsides)
if we go back to the interface way, here is an example of how you could use it