1

When trying to access an enum, called Monsters, from another class, Visual Studio gives me the error "The type or namespace name 'Monsters' could not be found (are you missing a using directive or an assembly reference?)", despite having an associated using in my class.

Here is my enum code:

namespace Zadify.Enums
{
    public enum Monsters
    {
        Zombie,
        Skeleton,
        Mummy,
        Robot,
        Demon
    }
}

And here is my class I need to access it from:

using System;
using System.Xml.Serialization;
using Zadify.Enums;

namespace Zadify
{
    [Serializable]
    public abstract class Goal
    {
        //Properties, Other methods

        public void AssignMonsterData(int rank)
        {
            var random = new Random();

            var monsterTypeValues = Enum.GetValues(typeof (Monsters)); //Gives the error on "Monsters" here
            var monster = (Monsters) monsterTypeValues.GetValue(random.Next(rank - 1)); //Also gives the error on this "Monsters"

            //Do other stuff
        }

I later do a switch on monster, and it seems to be fine with every time I use Monsters.Zombie, Monsters.Skeleton, etc. I also have several other enums in Zadify.Enums that all work fine, and, in fact, I use the exact same code on them in this class.

So far I have tried changing the enum's namespace and adding a using to that new namespace, copying Monsters into another file, cleaning the project, and rebuilding a hundred times. I'm using Visual Studio 2010 and writing a Mono for Android app, though there is no Android code in these classes.

Visual Studio doesn't seem to have a problem until I build. Beforehand, intellisense works fine, if I take out the using Zadify.Enums; line, it asks if that's what I want to reference, and ctrl+clicking it brings me to the proper file.

If there is any other code I should provide, let me know.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
ZAD-Man
  • 1,328
  • 23
  • 42
  • 5
    Is `Zadify.Enums.Monsters` defined in a different assembly (read: Visual Studio Project) than `Zadify.Goal`? – Snixtor Feb 26 '13 at 03:35
  • 2
    did you try to use Zadify.Enums.Monsters? Are there any dll reference issues? This code compiles fine for me as-is btw. – allen Feb 26 '13 at 03:38
  • They are in the same project. :/ I'm not referencing any outside dlls. `Monsters.cs` is in a folder named `Enums`, and `Goal.cs` is just in the root. All the other enums I'm using in this class are in `Enums` too. – ZAD-Man Feb 26 '13 at 04:11
  • Oh, @allen, sorry, I didn't answer your first question. Yes, I tried using `Zadify.Enums.Monsters`. It gave the same errors. – ZAD-Man Feb 26 '13 at 04:41
  • Another thing I didn't mention (but have now updated my question) - Visual Studio doesn't seem to have a problem until I build. Beforehand, intellisense works fine, if I take out the using `Zadify.Enums;` line, it asks if that's what I want to reference, and ctrl+clicking it brings me to the proper file. – ZAD-Man Feb 26 '13 at 04:45

4 Answers4

1

@ZAD-Man, check your client profile settings.

  1. Right-click the project
  2. You should be on the Application tab
  3. Look at the Target framework drop-down

Is it set to .Net Framework xx Client Profile? I've run into weird issues before where Visual Studio defaults my projects to that... try changing it to .Net Framework 4 without the client profile part and see if it works.

The screen you're looking for is similar to this:

enter image description here

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Ah, yes, actually, I had thought of that the Target Framework being off somehow, but this is where it being a `MonoDroid` project makes things a bit tricky...it has its own profile (apparently very similar to Silverlight's), though the only way I've seen that make a problem is when trying to reference 4.0 dlls and things like that. Anyway, it gives me the option of which minimum Android version to target, but I can't really change that now and I doubt it has to do with it anyway. – ZAD-Man Feb 26 '13 at 06:07
  • Thanks for the attempt to help! It turns out I just made a dumb mistake, one I've made before, actually, but didn't learn from, I guess. :P See my answer. – ZAD-Man Feb 26 '13 at 22:34
0

The reason it could be happening is that the class is not being added to the classlibrary. Also check if references has this classlibrary added to it.

Travis G
  • 1,592
  • 1
  • 13
  • 18
  • How might I go about checking those two things? – ZAD-Man Feb 26 '13 at 04:12
  • In Solution Explorer you will find the References node under the project node.In References you will be able to find classlibraries referenced in it. – Travis G Feb 26 '13 at 04:31
  • Well, this is all my own code, in the same project, so there isn't really anything to reference. – ZAD-Man Feb 26 '13 at 04:40
  • It has been seen many a time that Intellisense seems to work fine but build fails. As your Monsters.cs is in ENUMS folder, can you please add the reference to this classlibrary folder and try to build it. – Travis G Feb 26 '13 at 05:13
  • @sneakthief perhaps this is a similar issue ? [link]http://stackoverflow.com/questions/155105/how-come-classes-in-subfolders-in-my-app-code-folder-are-not-being-found-correct – Travis G Feb 26 '13 at 05:28
  • Thanks for the attempt to help! It turns out I just made a dumb mistake, one I've made before, actually, but didn't learn from, I guess. :P See my answer. – ZAD-Man Feb 26 '13 at 22:35
0

So it turns out I just made a dumb mistake. I had Goal.cs added as a link in another project, but forgot to link my enums. The only reason the other didn't have a problem was because it was getting caught on Monsters and not looking at the others. Since it was a link , clicking the exception just brought me to the real Goal.cs, so I thought the problem was in that project, but if I'd looked closer I would have seen it was the other project.

ZAD-Man
  • 1,328
  • 23
  • 42
0

I had this. The problem was the referencing project had accidentally had its target changed to x86, whereas the enum project target was still Any CPU.

Markus
  • 1,020
  • 14
  • 18
  • This is definitely something good to check for. In my case, it was just due to missing the reference entirely, but I think most people would encounter this due to a problem like yours, not mine. – ZAD-Man Nov 06 '19 at 20:12