简体   繁体   中英

C# cross class enum visibility - Possible?

so i have a class ClassA that contains an enum MyEnum, and a class ClassB that references that class (different Projects) and so in ClassB i have a

using ClassA;

clause and i can access that enum using something like

MyEnum value = MyEnum.EnumValue;

Now on a third project i have my Windows form and it has a clause like

using ClassB;

Now what can i add in ClassB to acess that enum on my windows Form? Is it even Possible? i would like to avoid having to add ClassA to my form just to access an enum.

The idea is that ClassB is sort of a manager between my form and the functionality in ClassA - but i would like to get access to that enum as it makes a lot of tasks easier

The logical thing in this situation is to make the enum independent of ClassA and make it public so that it is available throughout your solution.

Edit

After reading the comments, if you really want to avoid changing ClassA you could try to create a second enum and just cast one value to the other. For example, in an extension method:

public enum EnumB
{
    // make it equivalent to enum in ClassA
}

public static EnumB ToEnumB(this EnumA enumAValue) 
{
    EnumB newValue = (EnumB)(int)enumAValue;
}

i would like to avoid having to add ClassA to my form just to access an enum

You can't. If it is defined in the assembly where ClassA resides, you must reference that assembly.

If you are not talking about classes that live in different assemblies, you can put the enumeration in a file of its own and declare it as public - this will give access to it from any point in your code (which is normal practice for enumerations).


Update:

You can create an additional project containing shared objects, such as public enumerations and interfaces and reference that in your other projects.

Using clause is just a shortcut to avoid writing a namespace (or in this case a class) everytime you need to reference an element defined there.

If you don't like to write

ClassA.MyEnum..

you must write

using ClassA;
.....
 My Enum....

This is the way to access to ClassA.MyEnum from your form.
Anyway, if you don't like this solution, you can't re-define the Enum in another place.
You could create another enum called ClassB.MyEnum and then map any value of a ClassB.MyEnum to a ClassA.MyEnum. Here a sample for that workaround, I don't know if it's useful.

public class ClassB
 {
  public enum MyEnum
    {
     MyValue1,
     MyValue2
     }

   public static MyEnum Convert (ClassA.MyEnum originalValue)
    {
     return (MyEnum)Enum.Parse (typeof (MyEnum), originalValue.ToString());
    }
 } 

I suggest to move MyEnum outside ClassA.

I upvoted you all the other day since all you comments aided me in finding the best solution for my case.

The solution i ultimatly followed was to refactor everything so that i managed to get all code that required that enum (or anything else) from ClassA into ClassB and have ClassB report back to my main project through events the results of the operations so that i could update the UI with the results as they come in.

I think this is ultimatly the best solution for this problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM