C
C#2y ago
0_00

how to set enum type based on what child class is used

I have the public enum
public enum WorkerType{
Worker,
Associate,
Supervisor
}
public enum WorkerType{
Worker,
Associate,
Supervisor
}
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
public void setWorkerType(){
if (class = class1){
this.WorkerType = WorkerType.Worker
}
public void setWorkerType(){
if (class = class1){
this.WorkerType = WorkerType.Worker
}
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
Pobiega
Pobiega2y ago
Not sure I follow exactly, but one way to solve this is to have your classes specify the enum lemme show an example
public enum WorkerType
{
Worker,
Associate,
Supervisor
}

public interface IWorker
{
WorkerType WorkerType { get; }
}

public class Worker : IWorker
{
public WorkerType WorkerType => WorkerType.Worker;
}
public enum WorkerType
{
Worker,
Associate,
Supervisor
}

public interface IWorker
{
WorkerType WorkerType { get; }
}

public class Worker : IWorker
{
public WorkerType WorkerType => WorkerType.Worker;
}
you can replace the interface with a baseclass, if you want
0_00
0_00OP2y ago
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
Pobiega
Pobiega2y ago
hm? no thats.. thats not at all what I suggested :p
0_00
0_00OP2y ago
ok wait Im probably confused then
Pobiega
Pobiega2y ago
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
public class Worker : IWorker
{
public WorkerType WorkerType => WorkerType.Worker;
}
public class Worker : IWorker
{
public WorkerType WorkerType => WorkerType.Worker;
}
is an example class that implements that interface, and that there is the property and its getter in Java, this would be...
public class Worker implements IWorker {
public WorkerType getWorkerType() {
return WorkerType.Worker;
}
}
public class Worker implements IWorker {
public WorkerType getWorkerType() {
return WorkerType.Worker;
}
}
0_00
0_00OP2y ago
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
Pobiega
Pobiega2y ago
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
public abstract class BaseWorker
{
public abstract WorkerType WorkerType { get; }
}

public class Worker : BaseWorker
{
public override WorkerType WorkerType => WorkerType.Worker;
}
public abstract class BaseWorker
{
public abstract WorkerType WorkerType { get; }
}

public class Worker : BaseWorker
{
public override WorkerType WorkerType => WorkerType.Worker;
}
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
public class Program
{
public static void Main()
{
var list = new List<IWorker>
{
new Worker(),
new Supervisor(),
};

foreach (var worker in list)
{
Console.WriteLine(worker.WorkerType);
}
}
}
public class Program
{
public static void Main()
{
var list = new List<IWorker>
{
new Worker(),
new Supervisor(),
};

foreach (var worker in list)
{
Console.WriteLine(worker.WorkerType);
}
}
}
Want results from more Discord servers?
Add your server