- Home /
C# keyword extern
I have enum types that are required in three C# files. One file inherits from one file where the inherited file contains the declaration. Therefore the remaining file also requires a reference to the enum types.
Would I use: "extern public enum enumName" or is there another method?
Thanks CSDG
Answer by TheDemiurge · May 21, 2011 at 05:56 AM
with c#, most declarations / definitions made in one script are seen by the others. but check this if you're having trouble getting something to be recognized elsewhere and you've already made the actual definition public.
Answer by Statement · May 21, 2011 at 09:21 AM
Extern is used when you bind with external dlls, not for referencing an enum within the same project.
Here's an example of how you can access an enum defined inside another class' scope:
class Foo
{
public enum Bar
{
A, B, C
}
void Test()
{
Bar bar = Bar.A;
}
}
class Example
{
void Test()
{
Foo.Bar bar = Foo.Bar.B;
}
}
Notice how you had to type out the class name before the enum name to access it from the other class.
Another version would be to put the enum outside the class all together:
enum Bar
{
A, B, C
}
class Foo
{
void Test()
{
Bar bar = Bar.A;
}
}
class Example
{
void Test()
{
Bar bar = Bar.B;
}
}
Notice that we now can use the enum much more easily. All classes can use the enum without having to add any special code around it.
Answer by UniteMage · May 21, 2011 at 01:20 PM
Thanks for the replies.
Still trying to learn the tags.... NOT WORKING.
The problem, there were several classes: a FSMState class, a FSMSystem class, and a NPCControl class. Yet there were two files. The file FSMMachine.cs that contained the two former classes. In this file, I had to use the namespace FSMMachine.
In the other file, the NPCControl.cs contained the keyword "using" for the namespace that I had used. The FSMMachine.cs was not attached to anything, the NPCControl.cs was attached to my NPC GameObject.
The FSMState class was an abstract class, Unity would not allow me to attach it to a GameObject. The FSMSystem class was the Finite State machine. So these two classes were placed in the same file so other GameObjects could have access. This cut down any code redundancy. So all I had to worry about was the NPC control and its states.
The enumeration was also placed in the namespace FSMMachine. By using the namespace, everything was carried over into NPCControl including the enumeration.
Thanks again CSDG
abstract classes can't be instantiated as you know, and only classes that derive from $$anonymous$$onoBehaviour can be attached to GameObjects, so the abstract one definitely cannot derive $$anonymous$$B. if one file defines the enum within namespace name {..} , the others should be able to use it, even if they also define more code inside namespace name {..}.
If you've fixed everything, though, please mark your own answer as chosen.
Thanks
I am still learning the Unity Engine, $$anonymous$$onoDevelop and C#.... C# is a strange beast. It seems to have incorporated ideas from several program$$anonymous$$g languages. You ought to see me debugging this FS$$anonymous$$. I was pulling my hair out. I finally had to google all the C# PDFs I could find on the internet. The more I learn C#, the more I know this strange beast.
CSDG
@TheDemiurge, you are wrong. The abstract one can definitely inherit from $$anonymous$$onoBehavior. What matters is that $$anonymous$$onoBehavior is inherited at some point in the inheritance chain. Since it is an abstract class, you should create your own states that inherit from that abstract base class, or use any classes that were included. If the enum is in a namespace you aren't using, you have to add using ThatNameSpaceName; to your scripts that want to use that enum.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can You Play External MP3's from a script? 1 Answer
Keeping Unity Focused 0 Answers