News:
|
Public Enum Action
Start
Stop
End Enum
|
public enum EmployeeType
{
Boss,
Other
}
public bool GetsBigBonusAtEndOfYear(EmployeeType e)
{
switch(e)
{
case Boss:
return true;
case Other:
return false;
default:
return false;
}
}
public abstract class EmployeeType
{
public abstract bool IsEligibleForBonus();
private class BossType : EmployeeType
{
public override bool IsEligibleForBonus()
{
return true;
}
}
private class OtherType : EmployeeType
{
public override bool IsEligibleForBonus()
{
return false;
}
}
public static readonly EmployeeType Boss = new BossType();
public static readonly EmployeeType Other = new OtherType();
}
// neue Variante, macht die Methode eigtl. überflüssig
public bool GetsBigBonusAtEndOfYear(EmployeeType e)
{
return e.IsEligibleForBonus();
}
|
class Program
{
enum ControlActions
{
Start,
Stop
}
static void ExecuteAction(ControlActions action)
{
switch (action)
{
case ControlActions.Start:
Console.WriteLine("Starting ...");
break;
case ControlActions.Stop:
Console.WriteLine("Stopping ...");
break;
default:
Console.WriteLine("Upps ??? value: {0}, isDefined: {1}",
action.ToString(),
Enum.IsDefined(typeof(ControlActions), action));
break;
}
}
static void Main(string[] args)
{
ExecuteAction(ControlActions.Start);
string theAnswer = "42";
var theAction = (ControlActions)Enum.Parse(typeof(ControlActions), theAnswer);
ExecuteAction(theAction);
ExecuteAction(ControlActions.Stop);
Console.ReadLine();
}
}
Starting ...
Upps ??? value: 42, isDefined: False
Stopping ...
|
Wobei noch anzumerken wäre, dass man in diesem Fall auch so etwas wie 42 übergeben kann, da Start und Stop eigentlich ein Integer sind.
– Tosch 21.03.2014
|
||
@Tosch: Ja, dies ist eine Unschönheit bei VB.NET. Dasselbe in C# wird vom Compiler abgefangen, wenn Du keinen expliciten Cast machst.
– Roland Bär 21.03.2014
|
||
1 |
Dann sollte man Parameter prüfen:
If Not ([Enum].IsDefined(GetType(Action), value)) Then Throw New Exception("aktion") End If – lbm1305 21.03.2014
|
|
Die Variante, dass dies der Compiler prüft, gefällt mir trotzdem besser... :-)
– Roland Bär 21.03.2014
|
||
1 | ||
Hm, was fängt der Compiler da ab? Auch wenn der Übergabeparameter ein Enum ist, kann ich da problemlos "Unsinn" hineinreichen! Das Enum.Isdefinded ist die einzig sichere Lösung.
Ich könnte per Enum.Parse ihm die 42 reinreichen und der Code hätte ein Problem ;) – Xantiva 21.03.2014
|
||
Hmpf, Kommentare editieren wäre schon, um seine peinlichen Tippfehler zu korrigieren :(
=> IsDefined – Xantiva 21.03.2014
|
||
@Xantiva: Bei C# fängt der Compiler ab, wenn Du einen int anstelle eines enums übergibst (ohne cast). Darauf bezog ich mich beim Kommentar "Die Variante, dass dies der Compiler prüft, gefällt mir trotzdem besser..."
Dies ist aber leider bei VB.NET meines Wissens nicht möglich. – Roland Bär 21.03.2014
|
||
Ja, wenn ich einen int übergebe. Aber ich kann auch ein Enum Wert übergeben, der nicht "valide" ist. Wird etwas OT, aber ich poste es als Antwort, damit der Code lesbar ist.
– Xantiva 21.03.2014
|
Mit klicken auf den Haken bei meiner Antwort kannst Du in diesem Fall die Frage als beantwortet markieren.
Und Deine Rückmeldung wäre als Kommentar bei meiner Antwort besser platziert gewesen :-)