How to create logic in an enum - c#

In C# I know we can't we assign objects to an Enum. The functionality I'm after is when an Enum is declared, it triggers off an event.
So instead of having
enum MyEnum
{
string, int, etc
}
I could have
enum MyEnum
{
classType1, classType2
}
This would then also allow the classes classType1/classType2 constructor to be called which could (for example) be useful for logging for when the enum is declared.
Another way of presenting my issue could be
enum MyEnum
{
string1
{
//logic
},
string2
{
//logic
}
}
Is there a work around for this?

Instead of enum you can use a static class with static properties, it can be used same as an enum but you can write code inside getter and setter.

U could use properties instead of enum (then u can write ur own logic which will be called after/before assigning the variable). The enum structure should be used to create flags-like stuff.
private string _myVar;
public string MyVar
{
get { return _myVar; }
set
{
// logic 1 here
_myVar = value;
// logic 2 here
}
}

No work around as, I think, your expectation does not match the language. For example consider what would happen with:
if (myEnumValue == MyEnum.classType1)
Your question implies that on the right a class of 'classType1' would be instantiated for the equality test and then the result would depend on that types implementation of equality. This is confusing when the item on the left is an enum ... but with your assumption it is an object of type 'classType1' which implies that both the left and right are temporary objects.
You can see that this could not work.
But ... what I think your really after is a factory to create objects from an enum. Whole other question that raised other questions (sorry). An enum like this implies a state ... so if you have a state why does that state need an enum? A big question in itself, check out the state pattern.
Hope I've helped.

An enum is a Value type based on an Int## type. So this is the same as asking: can I get some logic into the assignment of int i = 7; and the direct answer is No.
Neither can you base an enum on anything other than an integer type.
But your requirement seems to be with tracking instances. That's easy with properties. But you can only do it for a specific property, not build it into the Type.

Enums are barely integers with comprehensive labels. As far as I know, what you are looking for cannot be done using enums.
However, and as stated by #Grumbler85, this behavior can be simulated using factories.
A factory is a special type of object that are used to create instances of other objects.
The easiest way to implement a factory is using a switch statement but other ways exists (reflection for example). Here's a simple example of what you are looking for:
Class A
{
...
}
Class B
{
...
}
enum eKnownTypes
{
A,
B
}
Class Factory
{
/*
Implement Singleton here
....
*/
public object CreateInstance(eKnownTypes t)
{
/*
Raise any event needed here
...
*/
switch (t):
{
case eKnownTypes.A: return new A(); break;
case eKnownTypes.B: return new B(); break;
}
return null;
}
}
/*
Set Event Handlers here
Factory.Instance.CustomEvent += new EventHandler ...
....
*/
A objectA = Factory.Instance.CreateInstance(eKnownTypes.A) as A;
...

You can do it with an enum, but you could do
public struct MyType
{
public const int OneValue = 1;
public const int TwoValue = 2;
private static readonly MyType one = new MyType(OneValue);
private static readonly MyType two = new MyType(TwoValue);
private readonly value int;
private MyType(int value)
{
this.value = value;
}
public static One
{
get { return this.one; }
}
public static Two
{
get { return this.two; }
}
public static implicit operator int(MyType source)
{
return source.value;
}
}
To give you a class that behaves like an enum but is fully extendable.
for instance, you can do
var myType = MyType.One;
switch (myType)
{
case MyType.OneValue:
...
case MyType.TwoValue:
...
default:
...
}
The instances are immutable and can be accuarately tested for equality using the implemenation inhereted from object, i.e. reference quality.

Related

Constrain a string parameter to be a constant from a specific class

This is probably an incredibly dumb question but: I have a function that takes in a string, and I want to make sure that the string is a constant from a specific class. Essentially the effect I'm looking for is what enums do:
enum MyEnum {...}
void doStuff(MyEnum constValue) {...}
Except with strings:
static class MyFakeStringEnum {
public const string Value1 = "value1";
public const string Value2 = "value2";
}
// Ideally:
void doStuff(MyFakeStringEnum constValue) {...}
// Reality:
void doStuff(string constValue) {...}
I know this can technically be achieved by doing some thing like
public static class MyFakeStringEnum {
public struct StringEnumValue {
public string Value { get; private set; }
public StringEnumValue(string v) { Value = v; }
}
public static readonly StringEnumValue Value1 = new StringEnumValue("value1");
public static readonly StringEnumValue Value2 = new StringEnumValue("value2");
}
void doStuff(MyFakeStringEnum.StringEnumValue constValue) {...}
But it feels kind of overkill to make an object for just storing one single value.
Is this something doable without the extra code layer and overhead?
Edit: While a enum can indeed be used for a string, I'd like to avoid it for several reasons:
The string values may not always be a 1:1 translation from the enum. If I have a space in there, different capitalization, a different character set/language, etc. I'd have to transform the enum in every function where I want to use it. It might not be a lot of overhead or a performance hit in any way, but it still should be avoided--especially when it means that I'm always mutating something that should be constant.
Even if I use a separate string array map to solve the above function, I would still have to access the translations instead of just being able to use the enum directly. A map would also mean having two sources for the same data.
I'm interested in this concept for different data types, ex. floats, ulongs, etc. that cannot be easily represented by enum names or stored as an enum value.
As for string -> enum, the point of using an enum in the first place for me is that I can rely on intellisense to give me a constant that exists; I don't want to wait until compile time or runtime to find out. Passing in an actual string would be duck typing and that's something I definitely don't want to do in a strongly typed language.
I would suggest you create an enum and parse the string value into an enum member.
You can use the Enum.Parse method to do that. It throws ArgumentException if the provided value is not a valid member.
using System;
class Program
{
enum MyEnum
{
FirstValue,
SecondValue,
ThirdValue,
FourthValue
}
public static void doStuff(string constValue)
{
var parsedValue = Enum.Parse(typeof(MyEnum), constValue);
Console.WriteLine($"Type: { parsedValue.GetType() }, value: { parsedValue }");
}
static void Main(string[] args)
{
doStuff("FirstValue"); // Runs
doStuff("FirstValuesss"); // Throws ArgumentException
}
}

Generics and objects conversion in C#

I have a class which is handling objects same way.
It's like:
class Handler<T>{
private T _obj;
public T obj{
get{
...//do sth
return _obj;
}
set{
...//do sth
_obj = value;
}
}
... // some other properties, no T anymore
}
There are large amount of code working on Handler objects, ignoring type. I mean, type T is not for them, there are setting other fields.
There are containers with Handler<> and so on.
At the end I need to return Handler with correct type.
I wanted to use Handler<object>, but there is no way I know to convert it to Handler<SomeClass>.
How can I handle situtations like this?
Why don't you make a base class for Handler<T>, which will contain all non-generic code?
class HandlerBase
{
// some other properties
}
class Handler<T> : HandlerBase
{
public T obj { ... }
}
If your "large amount of code" ignores T, than let it work with HandlerBase.
You can try with an IHandler interface.
IHandler<SomeClass> h1 = new Handler<SomeClass>();
IHandler<Object> h2 = h1;
This will work !
More info on Covariance on MSDN

Unexpected behavior with Struct internal values

*Solved. Thanks for the explanations guys, I didn't fully understand the implications of using a value type in this situation.
I have a struct that I'm using from a static class. However, the behavior is showing unexpected behavior when I print it's internal state at runtime. Here's my struct:
public struct VersionedObject
{
public VersionedObject(object o)
{
m_SelectedVer = 0;
ObjectVersions = new List<object>();
ObjectVersions.Add(o);
}
private int m_SelectedVer;
public int SelectedVersion
{
get
{
return m_SelectedVer;
}
}
public List<object> ObjectVersions;//Clarifying: This is only used to retrieve values, nothing is .Added from outside this struct in my code.
public void AddObject(object m)
{
ObjectVersions.Add(m);
m_SelectedVer = ObjectVersions.Count - 1;
}
}
Test code
VersionedObject vo = new VersionedObject(1);
vo.AddObject(2);//This is the second call to AddObject()
//Expected value of vo.SelectedVerion: 1
//Actual value of vo.SelectedVersion: 1
Now, if you test this code in isolation, i.e., copy it into your project to give it a whirl, it will return the expected result.
The problem; What I'm observing in my production code is this debug output:
objectName, ObjectVersions.Count:2, SelectedVer:0,
Why? From my understanding, and testing, this should be completely impossible under any circumstances.
My random guess is that there is some sort of immutability going on, that for some reason a new struct is being instanced via default constructor, and the ObjectVersions data is being copied over, but the m_SelectedVersion is private and cannot be copied into the new struct?
Does my use of Static classes and methods to manipulate the struct have anything to do with it?
I'm so stumped I'm just inventing wild guesses at this point.
Struct is value type. So most likely you are creating multiple copies of your object in your actual code.
Consider simply changing struct to class as content of your struct is not really good fit for value type (as it is mutable and also contains mutable reference type).
More on "struct is value type":
First check FAQ which have many good answers already.
Value types are passed by value - so if you call function to update such object it will not update original. You can treat them similar to passing integer value to function: i.e. would you expect SomeFunction(42) to be able to change value of 42?
struct MyStruct { public int V;}
void UpdateStruct(MyStruct x)
{
x.V = 42; // updates copy of passed in object, changes will not be visible outside.
}
....
var local = new MyStruct{V = 13}
UpdateStruct(local); // Hope to get local.V == 42
if (local.V == 13) {
// Expected. copy inside UpdateStruct updated,
// but this "local" is untouched.
}
Why is this a struct and not a class? Even better, why are you tracking the size of the backing store (List<T>) rather than letting the List<T> track that for you. Since that underlying backing store is public, it can be manipulated without your struct's knowledge. I suspect something in your production code is adding to the backing store without going through your struct.
If it were me, I'd set it up something like this, though I'd make it a class...but that's almost certainly a breaking change:
public struct VersionedObject
{
public VersionedObject()
{
this.ObjectVersions = new List<object>() ;
return ;
}
public VersionedObject(object o) : this()
{
ObjectVersions.Add(o);
return ;
}
public VersionedObject( params object[] o ) : this()
{
ObjectVersions.AddRange( o ) ;
return ;
}
public int SelectedVersion
{
get
{
int value = this.ObjectVersions.Count - 1 ;
return value ;
}
}
public List<object> ObjectVersions ;
public void AddObject(object m)
{
ObjectVersions.Add(m);
return ;
}
}
You'll note that this has the same semantics as your struct, but the SelectedVersion property now reflects what's actually in the backing store.

Implementing phantom types in C#

I'm looking to use "phantom types" to implement type-safe identifiers. There's a question here about doing this in F#.
I'd like to do this in C#. How?
I've got a solution (which has problems), so I'll post it as a possible answer to see if anyone can improve it.
Why not make it a sealed class with its constructor private?
public sealed class Id<TDiscriminator>
{
private Id() { }
//some static methods
}
I've come up with the following:
struct Id<TDiscriminator>
{
private readonly Guid _id;
private Id(Guid id)
{
_id = id;
}
public Guid Value
{
get { return _id; }
}
public static Id<TDiscriminator> NewId()
{
return From(Guid.NewGuid());
}
public static Id<TDiscriminator> From(Guid id)
{
return new Id<TDiscriminator>(id);
}
public static readonly Id<TDiscriminator> Empty = From(Guid.Empty);
// Equality operators ellided...
}
...which I can use as follows:
class Order { /* empty */ }
class Customer { /* empty */ }
void Foo()
{
var orderId = Id<Order>.NewId();
var customerId = Id<Customer>.NewId();
// This doesn't compile. GOOD.
bool same = (orderId == customerId);
}
I don't particularly want concrete classes for the discriminator, because I don't want anyone instantiating them.
I could get around that by using an interface or an abstract class. Unfortunately, these can still be derived from and instantiated.
C# won't let you use a static class as a type argument. I can't say that I'm totally happy with the answers to that question, because the answers basically say "just because".
How about?
public sealed class Order
{
private Order() {}
}
public static sealed class Id<T>
{
// ...
}
I think that's exactly what you say. No one (except some special cases) can construct it and no one can inherit from it.
Well, as far as I could understand, you would like to provide a mechanism for distinguishing different types by a custom identifier object. I think you are almost near a working solution. In .NET when having a generic class, each substitution of the generic argument (or each unique combination of the generic arguments, if more than one) creates a unique type in the runtime. In your code Id<Order> and Id<Customer> are two distinct types. The NewId() method returns an instance of Id<Order> for the orderId and Id<Customer> for the customerId variables. The two types do not implement the == operator and therefore cannot be compared. Moreover, such comparison would be difficult to implement, since you cannot determine all possible uses of the Id<TDsicriminator> - you cannot guess what type will the TDsicriminator be substituted with.
1
A fast and simple solution will be to do this:
class Order { /* skipped */ }
class Customer { /* skipped */ }
void Foo()
{
var orderId = Id<Order>.NewId();
var customerId = Id<Customer>.NewId();
bool sameIds = (orderId.Value == customerId.Value); // true
bool sameObjects = orderId.Equals(customerId); // false
}
Since the Value properties are both of the Guid type, comparison is possible.
2
If you need however, to implement the == operator, or some sort of equality comparisons for instances of Id<TDisciminator>, the approach will be different. What comes up to my mind is the following:
public abstract class IdBase
{
public abstract Guid Value { get; protected set; }
public static bool operator == (IdBase left, IdBase right)
{
return left.Value == right.Value;
}
}
public sealed class Id<TDiscriminator> : IdBase
{
// your implementation here, just remember the override keyword for the Value property
}
Many people would not recommend the second approach though, since different implementations of IdBase may happen to have the same Value property (if you used the constructor that passes an existing ID). For instance:
var guid = Guid.NewGuid();
var customerID = Id<Customer>.From(guid);
var orderID = Id<Order>.From(guid);
Here (customerID == orderID) will then return true which is probably not what you want.
Shortly, in such a case, two different types will count as equal, which is a big logical mistake, so I'd stick to the first approach.
If you need Id<Customer>.Value to always be different than Id<Order>.Value, because of the different generic arguments (Customer is different than Order), then the following approach will work:
public sealed class Id<in TDiscriminator>
{
private static readonly Guid _idStatic = Guid.NewGuid();
private Id()
{
}
public Guid Value
{
get { return _idStatic; }
}
}
Notice the in keyword used here. This is applicable for .NET 4.0 where generics can be covariant and ensures that your class uses contravariant generics. (see http://msdn.microsoft.com/en-us/library/dd469487.aspx). In the above code, the _idStatic field will have a unique value for every different type supplied as a generic argument.
I hope this info is helpful.

Enum of GUID's/Guid interop

I'm doing some interop and need to pass some GUID's. Is there a way to add a GUID attribute to an enum value and when it is marshalled it has the appropriate value?
Basically I'm trying to convert the code
#if !defined( STATIC_KSDATAFORMAT_SUBTYPE_PCM )
#define STATIC_KSDATAFORMAT_SUBTYPE_PCM\
DEFINE_WAVEFORMATEX_GUID(WAVE_FORMAT_PCM)
DEFINE_GUIDSTRUCT("00000001-0000-0010-8000-00aa00389b71", KSDATAFORMAT_SUBTYPE_PCM);
#define KSDATAFORMAT_SUBTYPE_PCM DEFINE_GUIDNAMED(KSDATAFORMAT_SUBTYPE_PCM)
#endif
(and several other similar ones)
and use them in an enum so I can specify the appropriate subtype format easily. Obviously I could probably just use a dictionary or some other similar method but I would like to make it as transparent as possible.
It would be nice to do something like
enum MyGuids : Guid
{
Guid1 = GUID("...") or just "..."
}
I can use a class/struct instead
static class MyGuids
{
public static Guid flag1 = new Guid("9ED54F84-A89D-4fcd-A854-44251E925F09");
}
But the only problem here is there is no way to relate the Guid in unmanaged structure to this class. It's specified as Guid and if I replaced it with MyGuids then it won't be a Guid any more. e.g., I lose type safety since any guid can end up in the field and not just the ones from MyGuid.
Any ideas?
Guids are structures in .NET, they are too large to fit in a simple value type by a factor of two. Sounds to me that you need a structure member initialized. That's going to need an assignment statement in your code. The const will work just fine for this. There is otherwise no way to get the compiler to do it automatically.
var sound = new foo();
sound.waveFormat = MyGuids.flag1;
// etc..
How about something like this?
void Main()
{
var dog = MyValues.Dog.ToId();
var cat = MyValues.Cat.ToId();
var bird = MyValues.Bird.ToId();
}
public enum MyValues
{
Dog,
Cat,
Bird
}
public static class Functions
{
public static Guid ToId(this MyValues value)
{
switch (value)
{
case MyValues.Dog:
return Guid.Parse("6d139d6a-2bfa-466d-a9a5-c6e82f9abf51");
case MyValues.Cat:
return Guid.Parse("AA139d6a-2bfa-466d-a9a5-c6e82f9abf51");
case MyValues.Bird:
return Guid.Parse("BB139d6a-2bfa-466d-a9a5-c6e82f9abf51");
default:
throw new InvalidDataException();
}
}
}

Categories