I have 2 files which are interacting with each other. I wanted to define an enum to make the code more readable, but if I define it in file 1, file 2 complains about having no knowledge of said enum. If I define ii in file 2, file 1 does the same. I am defining it as public too.
The solution was to define the enum in both files, but this doesn’t seem right to me. Not only is it redundant, but I fear it may cause some conflict, even if the types have the same items.
What is the veredict on this? Am I doing something wrong or worrying too much?
EDIT
Well, given the comments here I found an alternative which seems to be doing what I want without having to create a new file. I had:
file 1
class myClass1 { public enum MyEnum { ... } ... }
file 2
class myClass2 { public enum MyEnum { ... } .... }
Now, I have:
file 1
enum myEnum { ... } ... class myClass1 { ... }
file 2
class myClass2 { ... }
I didn’t want to create another file just for the enum, so this works for me. Well, as long as there is nothing wrong with it, which I think there isn’t.
You definitely shouldn’t define the enum in both locations. I recommend defining the enum in its own file with public accessibility. Then everyone should have no trouble accessing it. However, assuming that you want to define the enum in one of the two classes, I’ll continue…
You have to
importthe enum or use its fully qualified name. Assuming you are in the packagecom.stackoverflow, your classes should look like this, in the first file:and in another file: